how to store values in arrays?

Pages: 12
hi. im having trouble storing values in arrays. i have to enter them in one by one but they dont seem to get stored when i have to use the values in other functions.
Post a sample of code showing how you use them
this is the function i am trying to use them in:
1
2
3
4
5
6
7
void readthearrays (int score1[], int score2[], int score3[], int k) {
    int j;
                                                   
    for (j = 0; j < k; j++) {
        cin>> score1[17] >> setw(10) >> score2[17]>> setw(10)>> score3[17];
    }
}


and this is in the main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int score1[SIZE], score2[SIZE], score3[SIZE];
    int n = 17;
    int largest, smallest, howfarapart;
    
    cout<< "n is " << n << endl;
    cout<< "here is the original data" << endl;
    cout<< "array1" << setw(10) << "array2" << setw(10) << "array3" << endl;
    
    readthearrays(score1, score2, score3, 17);
    cout<< endl;
    return 0;
}
have you defined size yet? replace in the function you wrote look at line 5 and replace each 17 with j
i wrote this on top:
const int SIZE = 50;

line 5 is for another function that is not shown here. for that function i have to find the largest, smallest, and how far apart the large and small one are but i cant do that if my values are being stored.
1
2
for (j = 0; j < k; j++) {
        cin>> score1[17] >> setw(10) >> score2[17]>> setw(10)>> score3[17];

Since you're using a loop I believe you want to insert values in all of the elements of the arrays. In that case it is cin >> score[j], You are writing every value you input in the same position, the 18th (arrays start from 0), so the previous values are overwritten

I'm confused by cin >> setw(10). I didn't know you could use it for istreams
i want them to be spaced out and aligned under each other neatly.
this is what i want this part of the output to look like:
1
2
3
4
5
array1       array2         array3
34              23            12
54              45            54
76              34            47
123             3             345


i have to have 17 values under each array. is there a way the values can go like that automatically when i type it in instead of be pressing the spacebar after each one.
Last edited on
not in c++ unless you want to go really deep
ill stay away from the deep stuff for lol. so i would have to press space after typing them?
closed account (DETpfSEw)
i think setw(10) does not work with cin>>. you have to use it while you print arrays using cout<<.
moreover use itrator i in index of array while taking input.

1
2
3
4
5
6
7
void readthearrays (int score1[], int score2[], int score3[], int k) {
    int j;
                                                   
    for (j = 0; j < k; j++) {
        cin>> score1[j] >>  score2[j]>> score3[j];
    }
}
um.. im just starting to learn arrays so I'm not sure what you mean by "itrator i in index of array".
It's a typo. In loops 'i' is frequently used as index for arrays, but he meant "use j". Also it's not really an iterator, but an offset from the first element of the array
oh ok. so what am i doing wrong? is there a way of making them in three columns.
yes, whats ur operating system?
I'm using a macbook so im using xcode. codeblocks wasnt working.
good choice. i always felt code blocks was more windows favored. search i think theyre called x apps
what should i be looking out for?
Last edited on
its like win32 for mac
i think ill stick to pressing the space bars. i have a new problem now. i need to find the largest, smallest, and the range of the values. i think i have the correct function but maybe something is wrong in the main.
here is the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int findlargesmallrange (int numb[], int n, int &, int &, int &) {
    int largestsofar = numb[0];
    int smallestsofar= numb[0];
    int howfarapart;
    
    for (int j = 0; j < n; j++) {
        
        if (largestsofar < numb[j]){
            largestsofar = numb[j];
            return largestsofar;
        }
        if (smallestsofar > numb[j]){
            smallestsofar = numb[j];
            return smallestsofar;
        }
    }
    howfarapart = largestsofar - smallestsofar;
    
    return howfarapart;
}


and here is the main:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
int score1[SIZE], score2[SIZE], score3[SIZE], numb[SIZE], sumscores[SIZE];
    int n = 17;
    int largest, smallest, howfarapart;

 findlargesmallrange(numb, 17, largest, smallest, howfarapart);
    cout<< "largest is " << largest << endl;
    cout<< "smallest is " << smallest << endl; //i have to run this part 4 times for each array and the new array. 
    cout<< "they are " << howfarapart;
    cout<< " apart"<< endl << endl;
Last edited on
line one of your program you cant just have int&. you have to actually name the variable
Pages: 12