# Problem with C++ Program.

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
I am supposed to use arrays to create a program that removes the lowest number and highest number. Than it needs to add the rest but I can't even get my program to run.
The user needs to enter 8 scores:9.2,9.3,9.0,9.9,9.5,9.5,9.6,9.8.
the output should be:
contestant recieves: 56.90
Dropped low score: 9.00
Dropped High Score: 9.90

Any help to fix these errors will be appreciated. I'm not good at C++.
10 F:\Program6-Arrays2.0.cpp expected init-declarator before "int"
10 F:\Program6-Arrays2.0.cpp expected ,' or ;' before "int"
F:\Program6-Arrays2.0.cpp In function void getinput(int*, int)':
33 F:\Program6-Arrays2.0.cpp num' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
35 F:\Program6-Arrays2.0.cpp counter' undeclared (first use this function)
38 F:\Program6-Arrays2.0.cpp score' undeclared (first use this function)
39 F:\Program6-Arrays2.0.cpp sum' undeclared (first use this function)
F:\Program6-Arrays2.0.cpp In function int getlow(int*, int)':
49 F:\Program6-Arrays2.0.cpp numbers' undeclared (first use this function)
50 F:\Program6-Arrays2.0.cpp SIZE' undeclared (first use this function)
59 F:\Program6-Arrays2.0.cpp a function-definition is not allowed here before '{' token
59 F:\Program6-Arrays2.0.cpp expected ,' or ;' before '{' token
86 F:\Program6-Arrays2.0.cpp expected }' at end of input

Rich (BB code):
#include<iostream>
#include<cmath>
#include<conio.h>
#include<iomanip> //needed for setw()
using namespace std;
// Function prototypes
int getlow(int[],int)
int gethigh(int[],int)
double getsum(int[],int)
void getinput(int[],int)
void showoutput(int[],int)
int main()
{
int getlow,gethigh,numbers[SIZE];
double getsum,scores;
const int SIZE = 50;
void getinput(int[],int)
int getlow(int[],int)
int gethigh(int[],int)
double getsum(int[],int)
void showoutput(int[],int)

}
void getinput(int[],int)
{

cout << "Enter number of Judges Scores:";
cin >> num;
while (counter <= num )
{
cout << "Enter Judges Score #" <<counter << ":";
cin >> score;
sum+=score;
counter++;
}
}
int getlow(int[],int limit)
{
int  count;
int lowest;

lowest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count]<lowest)
lowest = numbers[count];
}

int gethigh(int[],int)
{
int count;
int highest;

highest=numbers[0];
for(count=1;count<SIZE;count++)
{
if(numbers[count]> highest)
highest=numbers[count];
}
double getsum(int[],int)
{
double scores;

scores+=score
scores-=lowest
scores-=highest
}
void output[int[],int]
{
cout<<setprecision(1)<<fixed<<showpoint;
cout<<"Contestant Receives: "<<scores<<endl;
cout<<"Dropped Low Score: "<<lowest<<endl;
cout<<"Dropped High Score: "<<highest<<endl;
system("pause");
}

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
New Code:
#include<iostream>
#include<cmath>
#include<conio.h>
#include<iomanip> //needed for setw()
using namespace std;
// Function prototypes
int getlow(int[],int);
int gethigh(int[],int);
double getsum(int[],int);
void getinput(int[],int);
void showoutput(int[],int);
int main()
{
int getlow,gethigh, numbers[SIZE];
double getsum,scores;
void getinput(int[],int);
int getlow(int[],int);
int gethigh(int[],int);
double getsum(int[],int);
void showoutput(int[],int);

}
void getinput(int[],int)
{

cout << "Enter number of Judges Scores:";
cin >> num;
while (counter <= num )
{
cout << "Enter Judges Score #" <<counter << ":";
cin >> score;
sum+=score;
counter++;
}
}
int getlow(int[],int limit)
{
int count;
int lowest;

lowest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count]<lowest)
lowest = numbers[count];
}

int gethigh(int[],int)
{
int count;
int highest;

highest=numbers[0];
for(count=1;count<SIZE;count++)
{
if(numbers[count]> highest)
highest=numbers[count];
}
double getsum(int[],int)
{
double scores;

scores+=score
scores-=lowest
scores-=highest
}
void output[int[],int]
{
cout<<setprecision(1)<<fixed<<showpoint;
cout<<"Contestant Receives: "<<scores<<endl;
cout<<"Dropped Low Score: "<<lowest<<endl;
cout<<"Dropped High Score: "<<highest<<endl;
system("pause");
}

#### thatoneguy

Joined Feb 19, 2009
6,359
Your errors are typos such as forgetting semicolon at the end of each line, and forgetting to declare and initialize variable types before using them. Remember the scope of variables, they may need to be re-declared in a function that is passed a variable.

Suggestion:
do a quicksort on the scores before anything else

The reason for sorting:
no need to find lowest and highest, if it is a sorted list, just sum the n+1 to n-1 entries and divide by n-2

sum array[1]to array[sizeof(array)-1] or similar context your compiler can handle. Assuming you array starts at [0], and the list is sorted, tossing out the first and last is just a matter of adjusting indexes, rather than re-arranging the array when the lowest is found and again when the highest is found.

Otherwise, if you don't want to sort, do pretty much what you are doing, except:

when looping through the array for lowest, also search for highest.

When adding, if number is ==lowest or number==highest, do not add to total sum.
When printing out the list, if number==lowest || number==highest do not print out
Divide by sizeof(array[])-2 for sample mean

Another way to find highest and lowest is to see if your compiler has max() and min() functions that would return the index of the highest and lowest values in an array.

After all of that, sorting them first seems faster, especially if the sample set gets large.

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
Thank you for all the input it has really helped with my code. I have fixed most of my small errors. I have been working on it today and have minimalized my errors. Sadly, I still have a few errors left I can't figure out how to solve.
Rich (BB code):
F:\Program6-Arrays2.8.cpp In function void getInput(int, int)':
47 F:\Program6-Arrays2.8.cpp invalid types int[int]' for array subscript
F:\Program6-Arrays2.8.cpp In function double getLow(double*, int)':
72 F:\Program6-Arrays2.8.cpp name lookup of counter' changed for new ISO for' scoping
57 F:\Program6-Arrays2.8.cpp using obsolete binding at counter'
Rich (BB code):
#include<iostream>
#include<cmath>
#include<conio.h>
#include<iomanip> //needed for setw()

using namespace std;

// Function prototypes

void getInput(int,int);
double getLow(double[],int);
double getHigh(double[],int);
double getsum(double[],int);

int main()
{
const int SIZE=8;
int num,numbers;
double sum,lowestscore,highestscore,scores[SIZE];

cout << setprecision(1) << fixed << showpoint;

getInput(numbers,SIZE);

lowestscore = getLow (scores,SIZE);

highestscore = getHigh (scores,SIZE);

sum = getsum (scores,SIZE);

sum -= lowestscore;

sum -= highestscore;

cout << "Contestant Receives: " << sum << endl;

cout << "Dropped Low Score: " << lowestscore << endl;

cout << "Dropped High Score: " << highestscore << endl;

system("pause");

}
void getInput(int numbers,int size)
{
int index;

for(index = 0; index <= size - 1;index++)
{

cout << "Enter Judges Score #" << (index+1) << ":";
cin >> numbers[index];
}
}
double getLow(double array[], int size)
{
double lowest;

lowest = array[0];

for(int counter = 1; counter < size ; counter++)
{
if( array [counter] < lowest)
lowest = array [counter];
return lowest;
}

double getHigh( double array[] , int size);
{
double highest;

highest = array[0];
for(counter = 1; counter < size ; counter++)
{
if(array [counter] > highest)
highest = array [counter];

return highest;
}
double getsum(double array[],int size);
{
double sum=0;
for(int count = 0 ;count < size ;count++)
sum += array [count];

return sum;
}
}
}

#### thatoneguy

Joined Feb 19, 2009
6,359
Thank you for all the input it has really helped with my code. I have fixed most of my small errors. I have been working on it today and have minimalized my errors. Sadly, I still have a few errors left I can't figure out how to solve.
Rich (BB code):
F:\Program6-Arrays2.8.cpp In function void getInput(int, int)':
47 F:\Program6-Arrays2.8.cpp invalid types int[int]' for array subscript
F:\Program6-Arrays2.8.cpp In function double getLow(double*, int)':
72 F:\Program6-Arrays2.8.cpp name lookup of counter' changed for new ISO for' scoping
57 F:\Program6-Arrays2.8.cpp using obsolete binding at counter'
The first one, int[int], you aren't declaring numbers[], only index in the getinput function
in function getLow, you've re-defined a global variable counter

Remember variable scopes, and once it compiles, step through to ensure it is doing what you think you are telling it to do.

What compiler/OS is this being written for?

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
The first one, int[int], you aren't declaring numbers[], only index in the getinput function
in function getLow, you've re-defined a global variable counter

Remember variable scopes, and once it compiles, step through to ensure it is doing what you think you are telling it to do.

What compiler/OS is this being written for?
The compiler this is being written for is "Dev-C++ 4.9.9.2".

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
I have fixed most the errors ,but my last error is
28 F:\Program6-Arrays2.8.cpp cannot convert double' to double*' for argument 1' to double getInput(double*, int)' .

I just need to really think,but I know i'll get it.

#### thatoneguy

Joined Feb 19, 2009
6,359
double is the actual value
*double is memory address that points to the memory location that holds the value of double, which can be passed by &double. The Ampersand means "The address of", while the * means "This is a pointer to"

When calling functions you need to be very clear if you are passing by value (double, the number 8323) or by reference (the address of double, &double, 0x0F439A5 (example), which holds the value 8323)

Hope that makes sense.

Thread Starter

#### chinesebarbiedoll

Joined Nov 9, 2011
9
Your really good at explanations. I am curious if I have to convert the var I used for the getinput function into an array seeing as how in my prototype I stated it as an array.

Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
Arrays always need to be passed by reference. When a function is called, the parameters are pushed onto the stack, and for very large arrays, that would result in a stack overflow, so just the base address is passed, as long as you declare the same array on "both ends" you are fine. "both ends" being function, function prototype, and function call all use the same datatype.

So even if you do function(myArray[24])

What is actually sent is function(&myArray) -> base address of array

then function needs to be prototyped to receive either *char or array[24] and the compiler should figure it out.

#### codehead

Joined Nov 28, 2011
57
Your really good at explanations. I am curious if I have to convert the var I used for the getinput function into an array seeing as how in my prototype I stated it as an array.
I'm a little confused, because your most recent listing of the code has you passing int to getInput (getInput(int,int)that seems that is an old listing, accidentally? Anyway, it's making me a little unsure of what code you are sayign is getting that compiler error message).

Anyway, C++ (and C) are pretty simplistic about arrays, including passing arrays, compared to some other language (no automatic bounds checking, etc.). If you declare an array such as "double myList[10];", then the array name (myList) is the base address of the array, which is that same as "&myList[0]" (read "address of the first element of the array myList"); myList, then, would be pointer to a double, otherwise known as "double *". So, you can have something like:

int numList[100];
fillList(numList, 100); // fill the array positions 0-99 with the values 0-99
...

// function to fill an array with the values that correspond to index
void fillList(int *numList, int count) {
for (int idx = 0; idx < count; idx++)
numList[idx] = idx;
}

There are other ways to say it, but I think this shows what's happening in C most clearly; the pointer to the start of the arraywhich is the first elementis passed to the function. The function knows the size of an int (usually 32 bitsin practice I avoid using "int" except in simple cases where it's always adequate and the exact size of the integer is unimportant). So, it knows that "numList[idx]" means to multiply "idx" by by the size of int in bytes (usually 4), add it to numList, and use the result as a pointer to the memory to access. C and plain C++ (no overriding of operators) don't even know how big the array is, so the code passes that info so that the function knows when to quit.

Did that help? If not, show me the code that went with the compiler message.

#### thatoneguy

Joined Feb 19, 2009
6,359
Rich (BB code):
double sum,lowestscore,highestscore,scores[SIZE];
//prototypes
double getsum(double[],int);
main()
{..
sum = getsum (scores,SIZE);
..
}

double getsum(double array[],int size);
{
double sum=0;
for(int count = 0 ;count < size ;count++)
sum += array
[count];
return sum;
}
These do not match, prototype and call to function. Add scores[SIZE] to function call so the function is aware of the number of array entries. You are passing an array (scores) and the size of that array (SIZE) as two separate items, when they are intertwined.

Your prototype and function declare that (scores[], size) will be passed to the function.

The function call doesn't do this, it only passes (scores, size) , The value of scores at whichver index it is currently at.

Calling the function as (scores[],size) will send &scores (by reference) to the base of the array, along with the number of elements in the array. If you forget the [], it sends the value of the current element in scores by value (on the stack), which doesn't match the function prototype or the function itself, so it won't compile.

When you DO get it to compile, do not expect it all to work flawlessly. These errors pointed out are the "Major" errors that prevented compilation. You will need to use a debugger to ensure that the vairables you are using have the expected value, either address pointer or literal. I know it gets confusing, especially when you are inside two loops and are calling pointers to pointers with **variable[x]. Once you understand them, it goes easy.

Just remember that [] means array which means passed by base address.
* means memory pointer, and the variable the * is in front of holds a memory address, and the value you want is the value that pointer's memory address holds, which is accessed by *variable

& means memory address, it points to the base of memory holding a string of characters, or an array of numbers. to get that array, you and the compiler need to know what is stored in it characters or integers, since finding the end uses a different method for each (for strings, when a \0 or NULL is found, that is the end of the string). For integers, the compiler uses sizeof(integer) and increments by that much for each value. If you try to read beyond the end of the array, you hit protected memory, which causes a core dump in linux, or in windows, a seemingly unrelated blue screen 27 minutes later.

#### thatoneguy

Joined Feb 19, 2009
6,359
See my next post below, this example was horked.

Last edited:

#### codehead

Joined Nov 28, 2011
57
Not the code you intended? Either that or I don't understand your point, sorry...

Example of how easily one can hork stuff up using pointers....

Rich (BB code):
void main(void)
{
rpointer=&r;
printf("Size of r is %d,\n sizeof rpointer is %d\n\n", r, rpointer);
printf("Address of r= is %x\n", r);
printf("Actual number entered was: %f\n", b);
//printf("Value of **rpointer is %x\n", **rpointer);
*rpointer=&r;
printf("Size of r is %d,\n sizeof rpointer is %d\n\n", r, rpointer);
printf("Address of r= is %x\n", r);
printf("Actual number entered was: %f\n", b);

}
You would expect some silly output, right? Think it through....

The actual output (on a rather beefy 64 bit Linux machine):

Enter number:123456
Size of r is -1521393152,
sizeof rpointer is 939018640

Address of r= is 381a0000
Actual number entered was: 123456.000000

That's only the most basic of headaches when you get to pointers...

#### thatoneguy

Joined Feb 19, 2009
6,359
Not the code you intended? Either that or I don't understand your point, sorry...
That snipped was just to show how easily one can mess up when using pointers. It's just a slice of a larger file I give to people trying to understand pointers and references so they can see it giving them headaches in real world.

Looks like whacked a way a little too much of the code trying to simplify it

I just started over to show how pointers (*) and addresses (&) work, so it is understood what exactly is passed when sending a value to a function. I suppose I should add an array to it as well.

Rich (BB code):
main ()

// begining of instructions
{

// declare variables and set initial value to zero
float r=0,a=0,b=0;
float *rpointer;

// print on screen enter number
printf("Enter number:");

// scan for input
scanf("%f",&b);

// manipulate variables
//r=(1/b);
r=b;

//print addresses:
rpointer=&r;
printf("Size of r is %d,\n sizeof rpointer is %d\n", sizeof(r), sizeof(rpointer));
printf("Address of r= is %x\n", &r);
printf("Actual number entered was: %f\n", b);
printf("Value of *rpointer is %f\n", *rpointer);
printf("Value of *(&r) is %f\n", *(&r));
}`
Enter number:54321
Size of r is 4,
sizeof rpointer is 8
Address of r= is 4aeefb80
Actual number entered was: 54321.000000
Value of *rpointer is 54321.000000
Value of *(&r) is 54321.000000

Last edited:
Similar threads