9 Replies - 1183 Views - Last Post: 26 July 2009 - 09:28 PM Rate Topic: -----

#1 derekc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-June 09

Compiles, works, professor says doesn't!

Posted 23 July 2009 - 11:36 PM

//This program asks the user for the number of tests, creates an array based on that response
//the user enters his scores and then the program lists the scores in ascending order, finds the average
//and then finds the average of the scores with the lowest one dropped
#include <iostream>
#include <iomanip>
using namespace std;

void arrSelectSort (int *[], int); //to sort the scores in ascending order
void showArray(int [], int);//show the test scores in ascending order
void getAverage (int [], int); //to get the average score without dropping the score
void dropScore (int [], int); //to get the average score while dropping the lowest score

int main()
{
int numScores;//the variable for the number of test scores to add!
int *scores;// to dynamically allocate and erray


cout << "How many test scores do you have to enter?" <<endl;
cin >> numScores; // Asking and storing the number of variables

scores = new int[numScores];// Dynamically create an array large enough to hold all of the test scores
////////////////////////////////////////this is what she says is wrong
int *arrPtr[numScores];
///she says you can't use a variable to define the size of an array
//
for(int count= 0; count < numScores; count++)//Inputting the number of test scores
{
cout << "Please enter the score for test #" << (count + 1) <<endl;
cin >> scores[count];
arrPtr[count] = &scores[count];
}


arrSelectSort(arrPtr, numScores);//Sort the scores using a array of pointers
showArray(scores, numScores);//Show the scores in ascending order
getAverage(scores, numScores);//Taking the average
dropScore(scores, numScores);//The average with the lowest score dropped

delete [] scores; //gotta free the memory
scores = 0;
return 0;
}

void arrSelectSort(int *arr[], int size)//num of elements of numScores
{
int startScan, minIndex, index;//putting the test scores in order
int *minElem;

for(startScan= 0; startScan < (size - 1); startScan++)//this is basically out of the book
{
minIndex = startScan;
minElem = arr[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (*(arr[index]) < *(arr[minIndex]))
{
minIndex = index;
}
}
if(startScan!=minIndex)
{
int swap = *(arr[startScan]);
*(arr[startScan]) = *(arr[minIndex]);
*(arr[minIndex]) = swap;
}

}
}

void showArray(int arr[], int size)//Display the array in ascending order
{
cout << "Test Scores"<<endl;
for(int counter = 0; counter < size; counter++)
{
cout << "Test # " << (counter + 1);
cout << "\t\t";
cout << arr[counter] << " ";
cout <<endl;
}
}
void getAverage(int arr[], int size)//Average test scores by adding all the values
//to one variable then dividing it by the number of scores
{
double sum = 0;//almost forgot to make this a double so we can have decimals!
double averagescore = 0;

for(int count = 0; count < size; count++)
{
sum += arr[count];
}
averagescore = sum*(1.0)/size;
cout <<"Average score:\t\t"<<averagescore<<endl;

}
void dropScore(int arr[], int size)// the same as the function above
//except we skip the first value in the array aka the lowest score
{
double sum = 0;
double average;

for(int count = 1; count < size; count++)
{
sum += arr[count];
}
average = sum*(1.0)/(size-1);
cout <<"Average with drop:\t"<< average <<endl;

}




Can someone please help me figure out why this won't work in more sensitive compilers? Works fine in the new dev c++? Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Compiles, works, professor says doesn't!

#2 Guest_c.user*


Reputation:

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 12:17 AM

////////////////////////////////////////this is what she says is wrong
/* int *arrPtr[numScores]; */

int **arrPtr;

if (!(numScores > 0))
	return 1;
	
arrPtr = new int *[numScores];

//she says you can't use a variable to define the size of an array



don't forget to do
delete [] arrPtr; in the end

formerly there was no ability to make array with non-constant size
only new standards have this feature as far as I know
Was This Post Helpful? 0

#3 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,190
  • Joined: 02-April 09

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 12:21 AM

There's no error checking, so you can enter 1 score and the average will be a divide-by-zero.
But it compiles cleanly using mingw, and I can't see anything obviously wrong.

-edit-
oh, actually, it would help if I read the previous comment, wouldn't it? Yes :) So ignore me.

This post has been edited by moopet: 24 July 2009 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 12:45 AM

I use defines for arrays all the time!

#define numScores 256
int **arrPtr;

if (!(numScores > 0))
	return 1;
   
arrPtr = new int *[numScores];



Was This Post Helpful? 0
  • +
  • -

#5 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 04:37 AM

your professor is right .. you can't,

  int n = 55;
  const int x = 33;

  int arr[n];	// error: fixed-size array need a constant value
  int array[x]; // correct

  int* N = new int[n];  // correct
  int* A = new int[x];  // correct too



fixed-size arrays need a fixed value, unlike dynamic arrays !
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 04:55 AM

Compiled fine with a GNU ansi compile. In fact, I haven't been able to get it to tank with any compiler.

I recall C (not C++) can have an issue with something like:
void testArray(int numScores) {
	int *arrPtr[numScores];
}



But I haven't been able to replicated it.

In any case, who cares? Just do this:
int **arrPtr = new int*[numScores];



Don't forget to delete. And, please, for the love of God, intent your code.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 junxuan   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 25-March 09

Re: Compiles, works, professor says doesn't!

Posted 24 July 2009 - 08:20 AM

if im not wrong, i remember visual studio not accepting this:

	int x = 1;
	int aray[x];



It requires x to be a constant.

the GNU GCC does accept it though.

This post has been edited by junxuan: 24 July 2009 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: Compiles, works, professor says doesn't!

Posted 26 July 2009 - 06:06 PM

I looked in a book of Shildt, he wrote this feature was added in C99
C++ bases on C89

but when I did
 
#include <stdio.h>

int main(void)
{
	int n;
	
	n = getchar();
	
	int a[n];
	
	a[0] = 5;
	
	printf("%d\n", a[0]);
	
	return 0;
}



[[email protected] tmp]$ cc -Wall -std=c89 test.c -o test
[[email protected] tmp]$ ./test
234
5
[[email protected] tmp]$


there were no warnings but although they must be, like for default int
when you do auto n;
Was This Post Helpful? 0

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Compiles, works, professor says doesn't!

Posted 26 July 2009 - 07:36 PM

The C++ standard requires a const int expression for the array size, but doesn't forbid a compiler from accepting a variable. So the GCC compiler extends the standard and accepts a variable array-size parameter, UNLESS you compile like this:
g ++ -pedantic sample.cpp
(without the blank space between g and ++.)
If you do that it will give an error message:
sample.cpp:25: error: ISO C++ forbids variable-size array `arrPtr'

Was This Post Helpful? 1
  • +
  • -

#10 Guest_c.user*


Reputation:

Re: Compiles, works, professor says doesn't!

Posted 26 July 2009 - 09:28 PM

cc -pedantic -std=c89 test.c -o test
ok, there are two warnings for variable length of an array and mixed declarations like
int a; a = 1; int b; for C code; and for -std=c99 is silent

This post has been edited by c.user: 26 July 2009 - 09:30 PM

Was This Post Helpful? 0

Page 1 of 1