need help with printing arrays?

hi. im having trouble printing arrays and spacing them out equally. i want them to look like this:
1
2
3
4
5
6
array 1     array 2      array 3
4             2            3
4             2            3
4             2            3
4             2            3
4             2            3


these numbers are made up and i have to have 17 numbers in each row. a function has to print out these numbers. they have to be entered manually.
i also have to print the three arrays separately in another function, it should look like this:
1
2
3
4
5
here is the score1 array:
4 4 4 4 4 
4 4 4 4 4             //they have to aligned like this. 5 a row. 
4 4 4 4 4 
4 4 

here is what i have for my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    int score1[SIZE], score2[SIZE], score3[SIZE];
    int n= 17;
    
    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<< "here is the score1 array" << endl;
    printarray(score1, 17);

    cout<< "here is the score2 array" << endl;
    printarray(score2, 17);
    
    cout<< "here is the score3 array" << endl;
    printarray(score3, 17); 
    
    return 0; 
}


and here are my two functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this function reads the arrays
void readthearrays (int score1[], int score2[], int score3[], int k) {
    int j;
                 //i have to make the 3 arrays with 17 numbers print from this array.   
    for (j = 0; j < k; j++) {
        cin>> score1[17] >> setw(10) >> score2[17]>> setw(10)>> score3[17];
    }

    return;
}

//this function prints the arrays. 
void printarray (int nums[], int k) {

    int j;     //i have to print the scores separately using this function and they have to be 5 numbers in a row. 
    for (j = 0; j < k; j++) {
        cout<< j << setw(5) << j << endl;

    }
    return; 
}


thanks.
Last edited on
In line 17 why are you printing j twice?
Your appearance and alignment objectives seem contradictory. Please explain further.
oh. i have tried to fix this myself and i have a new code. now the problem is that the in the "here is the score1 array" part the 17 numbers arent printed its only the first number in each array that is printed 17 times.
this is my function:
1
2
3
4
5
6
7
8
9
10
11
12
void printarray (int nums[], int k) { 

    int j;
    for (j = 0; j < k; j++)
    {		
        cout << setw(5) << nums[k];
        
		if (j % 5 == 4)
			cout << endl;
    }
    
}


and this is my main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    
    cout<< "here is the score1 array" << endl;
    printarray(score1, 17);
    cout<< endl;
    
    cout<< "here is the score2 array" << endl;
    printarray(score2, 17);
    cout<< endl;
    
    cout<< "here is the score3 array" << endl;
    printarray(score3, 17);
    cout<< endl;


this is the function that reads in the arrays:
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];
    }
}
try printf because it (in my opinion) makes it easier to format and set text
If you know all the array names and values will be less than 8 chars long, you can just put in << "\t" to tab to the desired distance apart.

If the values will be larger than 8 chars make a if loop to check the size.
example if less than 8, cout << "\t\t\t" else cout << "\t\t"
If you want 5, or 10 spaces between each char you could create a string of spaces and output those.
1
2
3
string shorttab= "     ";  // 5 spaces
string longtab= "          ";  // 10 spaces
cout << longtab;

when I do this I usually cheat

1
2
3
4
Tabs	t	t	t	t	t
01234567890123456789012345678901234567890
array 1     array 2      array 3
4             2            3


one more note, if your going to center the values under array 2 and 3, then I would center the output under array 1 also.
Last edited on
Topic archived. No new replies allowed.