Problem with C++ Program.

Discussion in 'Programmer's Corner' started by chinesebarbiedoll, Dec 5, 2011.

  1. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. #include<iostream>
    3. #include<cmath>
    4. #include<conio.h>
    5. #include<iomanip> //needed for setw()
    6. using namespace std;
    7. // Function prototypes
    8. int getlow(int[],int)
    9. int gethigh(int[],int)
    10. double getsum(int[],int)
    11. void getinput(int[],int)
    12. void showoutput(int[],int)
    13. int main()
    14. {
    15.     int getlow,gethigh,numbers[SIZE];
    16.     double getsum,scores;
    17.     const int SIZE = 50;
    18. void getinput(int[],int)
    19. int getlow(int[],int)
    20. int gethigh(int[],int)
    21. double getsum(int[],int)
    22. void showoutput(int[],int)
    23.    
    24. }
    25. void getinput(int[],int)
    26. {
    27.      
    28. cout << "Enter number of Judges Scores:";
    29. cin >> num;
    30. while (counter <= num )
    31. {  
    32.   cout << "Enter Judges Score #" <<counter << ":";
    33.   cin >> score;
    34.   sum+=score;
    35.   counter++;
    36. }
    37. }
    38. int getlow(int[],int limit)
    39. {
    40.     int  count;
    41.     int lowest;
    42.    
    43.     lowest = numbers[0];
    44.     for (count = 1; count < SIZE; count++)
    45.     {
    46.         if (numbers[count]<lowest)
    47.            lowest = numbers[count];
    48. }
    49.    
    50.    
    51. int gethigh(int[],int)
    52. {
    53.     int count;
    54.     int highest;
    55.    
    56.     highest=numbers[0];
    57.     for(count=1;count<SIZE;count++)
    58. {
    59.     if(numbers[count]> highest)
    60.     highest=numbers[count];
    61. }
    62. double getsum(int[],int)
    63. {
    64.        double scores;
    65.        
    66.        scores+=score
    67.        scores-=lowest
    68.        scores-=highest
    69. }
    70. void output[int[],int]
    71. {
    72.      cout<<setprecision(1)<<fixed<<showpoint;
    73.      cout<<"Contestant Receives: "<<scores<<endl;
    74.      cout<<"Dropped Low Score: "<<lowest<<endl;
    75.      cout<<"Dropped High Score: "<<highest<<endl;
    76. system("pause");
    77. }
    78.  
    79.  
     
  2. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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");
    }
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  4. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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.
    Code ( (Unknown Language)):
    1.  
    2. F:\Program6-Arrays2.8.cpp In function `void getInput(int, int)':
    3. 47 F:\Program6-Arrays2.8.cpp invalid types `int[int]' for array subscript
    4. F:\Program6-Arrays2.8.cpp In function `double getLow(double*, int)':
    5. 72 F:\Program6-Arrays2.8.cpp name lookup of `counter' changed for new ISO `for' scoping
    6. 57 F:\Program6-Arrays2.8.cpp using obsolete binding at `counter'
    7.  
    Code ( (Unknown Language)):
    1.  
    2. #include<iostream>
    3. #include<cmath>
    4. #include<conio.h>
    5. #include<iomanip> //needed for setw()
    6.  
    7. using namespace std;
    8.  
    9. // Function prototypes
    10.  
    11. void getInput(int,int);
    12. double getLow(double[],int);
    13. double getHigh(double[],int);
    14. double getsum(double[],int);
    15.  
    16. int main()
    17. {
    18. const int SIZE=8;
    19. int num,numbers;
    20. double sum,lowestscore,highestscore,scores[SIZE];
    21.  
    22. cout << setprecision(1) << fixed << showpoint;
    23.  
    24. getInput(numbers,SIZE);
    25.  
    26. lowestscore = getLow (scores,SIZE);
    27.  
    28. highestscore = getHigh (scores,SIZE);
    29.  
    30.  
    31. sum = getsum (scores,SIZE);
    32.  
    33. sum -= lowestscore;
    34.  
    35. sum -= highestscore;
    36.  
    37. cout << "Contestant Receives: " << sum << endl;
    38.  
    39. cout << "Dropped Low Score: " << lowestscore << endl;
    40.  
    41. cout << "Dropped High Score: " << highestscore << endl;
    42.  
    43. system("pause");
    44.  
    45.  
    46.  
    47. }
    48. void getInput(int numbers,int size)
    49. {
    50. int index;
    51.  
    52. for(index = 0; index <= size - 1;index++)
    53. {
    54.  
    55. cout << "Enter Judges Score #" << (index+1) << ":";
    56. cin >> numbers[index];
    57. }
    58. }
    59. double getLow(double array[], int size)
    60. {
    61. double lowest;
    62.  
    63. lowest = array[0];
    64.  
    65. for(int counter = 1; counter < size ; counter++)
    66. {
    67. if( array [counter] < lowest)
    68. lowest = array [counter];
    69. return lowest;
    70. }
    71.  
    72.  
    73. double getHigh( double array[] , int size);
    74. {
    75. double highest;
    76.  
    77. highest = array[0];
    78. for(counter = 1; counter < size ; counter++)
    79. {
    80. if(array [counter] > highest)
    81. highest = array [counter];
    82.  
    83. return highest;
    84. }
    85. double getsum(double array[],int size);
    86. {
    87. double sum=0;
    88. for(int count = 0 ;count < size ;count++)
    89. sum += array [count];
    90.  
    91. return sum;
    92. }
    93. }
    94. }
    95.  
    96.  
     
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  6. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    The compiler this is being written for is "Dev-C++ 4.9.9.2".
     
  7. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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.
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  9. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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: Dec 6, 2011
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  11. codehead

    Member

    Nov 28, 2011
    56
    11
    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.
     
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
     
  13. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    See my next post below, this example was horked.
     
    Last edited: Dec 7, 2011
  14. codehead

    Member

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


     
  15. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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 :p

    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.

    Code ( (Unknown Language)):
    1.  
    2. main ()
    3.  
    4.         // begining of instructions
    5. {
    6.  
    7.         // declare variables and set initial value to zero
    8.         float r=0,a=0,b=0;
    9.         float *rpointer;
    10.  
    11.         // print on screen enter number
    12.         printf("Enter number:");
    13.  
    14.         // scan for input
    15.         scanf("%f",&b);
    16.  
    17.         // manipulate variables
    18.         //r=(1/b);
    19.         r=b;
    20.  
    21.         //print addresses:
    22.         rpointer=&r;
    23.         printf("Size of r is %d,\n sizeof rpointer is %d\n", sizeof(r), sizeof(rpointer));
    24.         printf("Address of r= is %x\n", &r);
    25.         printf("Actual number entered was: %f\n", b);
    26.         printf("Value of *rpointer is %f\n", *rpointer);
    27.         printf("Value of *(&r) is %f\n", *(&r));
    28. }
    29.  
    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: Dec 7, 2011
Loading...