-4

I do not get arrays, Im sure there are easier ways to make this program but I must do it the teacher's way and I am lost.

This is the assigment: Assignment

I do not get how I should go about these arrays. most confusing thing I seen by far. What I would like is a guide or help on how i should program these arrays or how I should program arrays period. Not asking to do the rest for me, I already know how to do most of this, its just the arrays I like to know how to do.

This is my current program:

#include<iostream>
using namespace std;

void getPoints(int pPossible[], double pEarned[], int numItems, int limit);
int sumArray(double



void getpoints(int pPossible[], double pEarned[], int numItems, int limit)
{
        int count;

    while (limit == limit)
    {
        cout << "Input grade points for a category in the gradebook: " << endl
            << "How many items available in the category?: ";
        cin >> numItems;
        cout << endl;

        if(numbItems > limit)
        {
            cout << "The Value exceeds the maximum number of items." << endl;
            continue;
        }
        break;
    }

    count=1;

    for(count=1; count<numItems; count++)
        cout << "Enter points then points possible for Item " << count << ": ";
        cin << pEarned[count] << pPossible[count];

   return 0;
}
Community
  • 1
  • 1
user2910452
  • 13
  • 1
  • 7
  • 1
    what is `int sumArray (double `, are you missing something ? – Moha the almighty camel Nov 27 '13 at 14:05
  • Seriously? while `foo == foo`? Is that a thing? Who taught you not to just use `while(true)` or `while(1)`? Should be: `if(numbItems >= limit)` reference parameters: (put `&` next to parameter declaration) http://stackoverflow.com/questions/2564873/reference-parameters-in-c-very-basic-example-please (avoid reference variables) Note: all parameters are outputs of function except `limit`. –  Nov 27 '13 at 14:11

1 Answers1

0

C++ array indexes are zero-based, so you should use zero for the initial value in the for loop. Also, you're missing the braces for the for loop body; as it is, it will run the cout line numItem-1 times and the cin line just once.

Another thing: the operators in the for's cin line should be >>, not <<. You want input here, not output.

Finally, like @ebyrob said, make numItems a reference parameter (it would be clearer for the caller to use a pointer instead, but the assignment asked for a reference parameter).

void getpoints(int pPossible[], double pEarned[], int& numItems, int limit)
{
    //Read number of items
    while (true)
    {
        cout << "Input grade points for a category in the gradebook: " << endl
            << "How many items available in the category?: ";
        cin >> numItems;
        cout << endl;    
        if(numItems >= limit)
        {
            cout << "The Value exceeds the maximum number of items." << endl;
            continue;
        }
        break;
    }
    //Read earned and possible for each item
    for(int count=0; count<numItems; count++)
    {
        cout << "Enter points then points possible for Item " << count << ": ";
        cin >> pEarned[count] >> pPossible[count];
    }
    return 0;
}
dario_ramos
  • 7,118
  • 9
  • 61
  • 108
  • Thank you, I get what it means on that end. But the next thing i do not get is step 2 of the instructions. I know its a function but I dont get its steps. "that indicates the number of elements used in the array parameter. The const modifier must be added to the array parameter. b. returns the sum of the first numberUsed elements of the array parameter." that to me doesnt say alot im guessing its some kind of partially filled array and/or const array. how is this set up? – user2910452 Nov 27 '13 at 14:52
  • Not really, you just pass the full array, but declare the array parameter as const. This prevents the function from modifying the array (which is OK since you just need to read its values). Then you iterate from zero to numberUsed-1 and add the values. – dario_ramos Nov 27 '13 at 14:57
  • About making an array parameter const, the easiest way would be to treat it as a pointer. (i.e. `double * const pValues` ). The relationship between arrays and pointers is a key concept. Read [this](http://www.functionx.com/cpp/Lesson14.htm) for starters. – dario_ramos Nov 27 '13 at 15:04
  • Actually, since an array parameter is actually a pointer, you're not passing a copy of the whole array to the function; you're just passing the address to its first element. Sorry if I misled you in my first comment. You'll get it when you read about arrays and pointers. – dario_ramos Nov 27 '13 at 15:07
  • Okay how do I do this to make this entire step 2 to compute the grades score. is it like I normally would but the variable used to compute would be sumArray(const double element, int number used) element is double type and would be the max points possible and numberUsed is the grade you got correct? if so I think i can do it, if im wrong, Please tell me why. – user2910452 Nov 27 '13 at 15:16
  • A pointer can be treated just like an array; that is, if you declare the array parameter as `double * const pArray`, you can do `cout << pArray[2]` to display the third element, for instance. But since it's const, if you try to do `pArray = 0` it won't build. Try it out yourself. – dario_ramos Nov 27 '13 at 15:19
  • More on `const`. If you declare the parameter as `double * const pArray`, you cannot assign to that memory address: the pointer is constant (i.e. something like `pArray = 0` won't build). But if you declare it as `const double * pArray`, then you cannot assign to the memory pointed by the pointer (i.e. something like `pArray[2] = 0` won't build). You can get both by declaring `const double * const pArray`, which would be the best for your case. – dario_ramos Nov 27 '13 at 15:26
  • okay that makes sense, then what about numberUsed, where does that take place in all this? – user2910452 Nov 27 '13 at 15:31
  • Like I said, you iterate over the array in a for loop, from 0 to numberUsed-1, adding the array values from each position to a variable which will be the function's return value. – dario_ramos Nov 27 '13 at 15:34
  • http://i231.photobucket.com/albums/ee74/itachi51056/Capture_zpsd250adaa.png is this the right way to set up? (i will fill in the actual program under each loop later) – user2910452 Nov 27 '13 at 15:52
  • Nope, wrong. Forget about item 1 when solving item 2. Think of `sumArray` as a generic function which does what item 2 asks. It's in item 3 when you'll use `getPoints` and `sumArray` in your `main` function. – dario_ramos Nov 27 '13 at 16:03
  • Also, in the for loops, it's `<`, not `==`. Using `==` you won't even get to enter the for body. That condition must be true to run another iteration, not to stop. – dario_ramos Nov 27 '13 at 16:05
  • how do you suggest I do it? in item 3 it says use sumArray to solve the scores and then on item 2 itself when im making it, it does whatever it says which im still having trouble understanding what item 2 tells me to do. – user2910452 Nov 27 '13 at 16:11
  • Item 2 is just as simple as [this](http://ideone.com/iykVEO). Items 1 and 2 are about defining standalone functions which comply to the given requirements. Item 3 is about using both of them to make an actual program. – dario_ramos Nov 27 '13 at 16:15
  • oh okay so your like separating the list of scores in the sumArray and then returning the sum of the scores as the total amount then you calculate the total in the main function am i correct? also explain your line where you used a function call in your cout. I never seen that before also is v[] supposed to be the scores? – user2910452 Nov 27 '13 at 16:23
  • Yes, that would be the way to use it in item 3. The cin line does indeed have a function call; the result is saved in a temporary storage and handed off to cout (the compiler takes care of that). And about `v`, it's just a generic array, which in item 3 will happen to be an array of scores. But `sumArray` has no idea about that; it can be adding scores, network metrics, speeds, whatever. And that's what makes it reusable. Another key concept. – dario_ramos Nov 27 '13 at 16:35
  • thank you very much dario. You helped me alot. Much appreciated – user2910452 Nov 27 '13 at 16:45