Displaying in rows and colums

hello all! i seem to have a problem. i am assigned to display 50 test scores in rows of 5. I have no idea what i am doing, i have found nothing useful on the interwebs on how to display in rows and cols. So i come to you guys! :D

line 48 is where it stars, i had just run it through a bubblesort to arrange in ascending order, i just need them to print out in rows of 5.

Any help would be appreciated! :)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <iomanip>
using namespace std;

void bubbleSort(double scores[], int numScores);

int main (void){
 double *scores, //To dynamically allocate an array
 total = 0, // Accumulator
 average; // To hold average scores
 int numScores, //To hold the number of test scores
 count; //Counter variable
 int row, col;

 //Get the number of test scores
 cout << "How many test scores would you like to enter? ";
 cin >> numScores;

 //Dynamically allocate an array large enough to hold that many test scores

 scores = new double[numScores];

 //Get the test scores
 cout << "Enter the test scores below.\n";
 for (count = 0; count < numScores; count++){
  cout << "Test Score " << (count + 1) << ": ";
  cin >> scores[count];
 }

 //Calculate the total of the scores
 for (count = 0; count < numScores; count++){
  total += scores[count];
 }

 //Calculate the average test score
 average = total / numScores;

 //Display the results
 cout << fixed << showpoint << setprecision(2);
 cout << "Average score is: " << average << endl;
 //find min and max.
 cout<<"Smallest grade:"<<*min_element(scores,scores+5)<<endl;
 cout<<"Highest grade:"<< *max_element(scores,scores+5)<<endl;
 

 bubbleSort(scores,numScores);
 for (count = 0; count < numScores; count++){
     for(row=0; row<5; row++)
     {
         cout<<row<<' ';
         for(col=0; col<10; col++){
             cout<<col;
         }
     }
  //cout<<scores[count]<<endl;
 }


 //Free dynamically allocated memory
 delete [] scores;
 scores = 0; //Make scores point to null.
 cin.ignore(INT_MAX, '\n');
 cin.ignore(INT_MAX, '\n');

 return 0;
}


void bubbleSort(double scores[], int numScores){
 bool exchanges;
 do{
  exchanges = false; // assume no exchanges
  for(int i = 0; i < numScores - 1; i++){
   if(scores[i] > scores[i + 1]){
    double temp = scores[i];
    scores[i] = scores[i+1];
    scores[i+1] = temp;
    exchanges = true; // after exchange, must look again
   }
  }
 }while(exchanges);
}



Thanks!
Try this

1
2
3
4
5
for (rows = 0; rows < 10; rows++) {
    for (cols = 0; cols < 5; cols++) {
         cout << scores[rows * 5 + cols] << "\t";
    }
}


and when you make it work, simply add column and row labels if you want (column labels should be in _separate_ cycle).
I have it displaying in rows now but it repeats over and over

1
2
3
4
5
6
7
8
9
 bubbleSort(scores,numScores);
 for (count = 0; count < numScores; count++){
     for (rows = 0; rows < 5; rows++) {
    for (cols = 0; cols < 10; cols++) {
         cout << scores[rows * 10 + cols] << "\t";
    }
}
  //cout<<scores[count]<<endl;
 }


1
2
3
12      13      13      14      15      16      17      21      23      23      23      24      25    26       27      28      29      30      32      34      34      35      36      36      37      43    43       43      43      43      43      45      46      47      49      50      54      54      54    54       55      56      56      57      67      76      80      85      97      443     12      13    13       14      15      16      17      21      23      23      23      24      25      26      27    28       29      30      32      34      34      35      36      36      37      43      43      43    43       43      43      45      46      47      49      50      54      54      54      54      55    56       56      57      67      76      80      85      97      443     12      13      13      14    15       16      17      21      23      23      23      24      25      26      27      28      29    30       32      34      34      35      36      36      37      43      43      43      43      43    43       45      46      47      49      50      54      54      54      54      55      56      

ect... 


thanks!
Try this:



1
2
3
4
5
6
7
for (int i = 0; i < 50; i++)
	{
	cout << num[i] << " ";
	if ((i + 1) % 10 == 0) //Everytime loop hits a 10th # moves to next row
	cout << endl;
	}
	cout << endl;
Last edited on
alright that does make it look a lot better but i am still getting repeats.

1
2
3
4
5
6
7
8
9
10
21 32 33 43 44 
45 54 55 66 67 

21 32 33 43 44 
45 54 55 66 67 

21 32 33 43 44 
45 54 55 66 67 

ect...


i changed it to rows of 5 and am testing it with 10 numbers. it prints them out perfectly but then repeats the print out.
You should just be able to eliminate the two dimensional array you created using nested for loops and replace it with the for loop above with that 'if' starement . If there are 50 test scores being held in an array.. I had a similar problem, but mine had to generate 50 random numbers.

I created a display function. Passed in the array and used that approach above.

Hope this helps...
Topic archived. No new replies allowed.