Array project

Hey everyone, I just started working with arrays and I'm trying to get the swing of the syntax still. Can anyone give me some pointers on this one..? I've already began coding below...


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Sample Output



Frame     Score     Game
  1         20        20
  2         17        37
  3          9        46
  4         29        75
  5         19        94
  6          9       103
  7         19       122
  8         20       142
  9         20       162
  10        19       181





Input:
   No data comes from the keyboard; all data, including the data
   for the array, are contained within the program.

   TODO #1: in the main() function,
      complete declaration of integer array of ten bowling scores.
      Compile-time array with initialization list of scores.
   
Processing and Output:
   TODO #2: in the main() function,
      complete the call to display() function
      -see prototype for parameter specifics

   TODO #3: code the complete display() function
      -see directions in function comments

*/


#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
   /* declarations ------------------------------------------------*/

   // constants
   const int SIZE = 10;

   // function prototype(s)
   void  displayScores(const int scores[], const int SIZE);

   // TODO #1: declare & initialize score array with scores above
   // array data: use data from Score column of sample output
   // STUDENT CODE BEGINS

   const int scores [9] = {20, 17, 9, 29, 19, 9, 19, 20, 20, 19};
   int scores;




   // STUDENT CODE ENDS

   /* statements   ------------------------------------------------*/

   // start the program


   // TODO #2: call display function, passing the array contents and size
   //   replace the two underlines with the correct parameters
   // STUDENT CODE BEGINS






   // STUDENT CODE ENDS



   // terminate the program
   cout << endl;
   cout << "*** end of 276Arrays_01.cpp program ***" << endl << endl;
   cin.get();

   return 0;
}   // end main()

/*--------------------------------------------------------------------//
// Function Name: displayScores()
// Parameters:       score :  const int[]
//                   SIZE  :  const int
// Return:  void
//
// Purpose: to display bowling scores and total by frame
//
// Notes:
//    use a 'for' loop
//    format (setw, left, right, etc) & display headings
//    format (setw, left, right, etc) & display frame details
//--------------------------------------------------------------------//
// TODO #3: code the display function
//   Suggested Algorithm

   declare local variables (if needed)
   format & print column headings
   display detail lines
   for each bowling frame  (use a 'for' loop)
       format & print frame number
       format & print frame score
       format & print game score
   end for loop
*/
      
// STUDENT CODE BEGINS






// STUDENT CODE ENDS
closed account (D80DSL3A)
You have a good start with line 58.
Things start going wrong quick though. You define a variable with the same name on line 59, creating a naming conflict. Just remove the 2nd variable.

You only need the one array you have declared already on line 58.
Everything can be done in the print() function using just the array (and its size) in a single for loop.
The 'frame number' is just the loop index+1. The game score is just the running sum of the score values in the array, so keep this sum updated in the for loop (sum+=scores[i]; each iteration). That gives everything needed to output the lines as required.

Hope that helps.
Last edited on
ok that definitely did help but now I'm able to compile everything but im getting the wrong values in for my frames..nothing but 1's and for the score nothing but the same number 20 but the game values are correct...odd...heres what I got now:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
   /* declarations ------------------------------------------------*/

   // constants
   const int SIZE = 10;

   // function prototype(s)
   void  displayScores(const int scores[], const int SIZE);

   // TODO #1: declare & initialize score array with scores above
   // array data: use data from Score column of sample output
   // STUDENT CODE BEGINS

    int scores [SIZE] = {20, 17, 9, 29, 19, 9, 19, 20, 20, 19};
   




   // STUDENT CODE ENDS

   /* statements   ------------------------------------------------*/

   // start the program
   cout << "*** start of 276Arrays_01.cpp program ***" << endl;
   cout << endl;


   // TODO #2: call display function, passing the array contents and size
   //   replace the two underlines with the correct parameters
   // STUDENT CODE BEGINS

   displayScores(scores, SIZE);




   // STUDENT CODE ENDS



   // terminate the program
   cout << endl;
   cout << "*** end of 276Arrays_01.cpp program ***" << endl << endl;
   cin.get();

   return 0;
}   // end main()

/*--------------------------------------------------------------------//
// Function Name: displayScores()
// Parameters:       score :  const int[]
//                   SIZE  :  const int
// Return:  void
//
// Purpose: to display bowling scores and total by frame
//
// Notes:
//    use a 'for' loop
//    format (setw, left, right, etc) & display headings
//    format (setw, left, right, etc) & display frame details
//--------------------------------------------------------------------//
// TODO #3: code the display function
//   Suggested Algorithm

   declare local variables (if needed)
   format & print column headings
   display detail lines
   for each bowling frame  (use a 'for' loop)
       format & print frame number
       format & print frame score
       format & print game score
   end for loop
*/
      
// STUDENT CODE BEGINS

void displayScores (const int scores[], const int SIZE)
{
   //declare variables
   int frame = 0;
   int game = 0;

   // column headings
   cout << setw(5) << "Frame";
   cout << setw(5) << " ";
   cout << setw(5) << "Score";
   cout << setw(5) << " ";
   cout << setw(4) << "Game\n";

   // Accumulate running total score & display scores
   for (int i = 0; i <= SIZE; i++)
   {
      game += scores[i];

      //column data
      cout << setw(3) << right << "1";
      cout << setw(11) << right << scores[0];
      cout << setw(10) << right << game << endl;
   }

}

// STUDENT CODE ENDS
You need to use the loop variable.
1
2
3
cout << setw(3) << right << "1"; // i+1 will print 1 the first time then increment each time i is
cout << setw(11) << right << scores[0]; // scores[i] same thing here
cout << setw(10) << right << game << endl;
thank you admkrk that fixed my scores column but on the fist cout I changed it to
cout << setw(3) << right << "i+1"; and all it printed in the frames column was " i+1 all the way down.../:
wow I'm even closer now I changed it up to cout << setw(3) << right << i+1; instead of with the quotes and that worked..!! Only now...I have one extra row showing a 11th frame with a very long - number next to it..., how do I get rid of this..?
That is because you're telling the for loop to loop 11 times.
1
2
3
const int SIZE = 10;

for (int i = 0; i <= SIZE; i++) // for(i = 0; i < SIZE; i++) 

You could initialize i to 1 and change first output to just i instead of i+1, but that makes it harder to read imho.

If you set break points and step through your code, you will be able to spot things like that easily. Or simply add an extra temporary output line.
1
2
3
4
cout << setw(3) << right << "1";
cout << setw(11) << right << scores[0];
cout << setw(10) << right << game << endl;
cout << i;  // will show that the loop doesn't end until i == 12 
Last edited on
So I just did what you said admkrk and it was close but it gave me this output now when I initialized i to 1 and on the first cout use i instead of the i+1
1
2
3
4
5
6
7
8
9
10
11
12
 

  1         17        37
  2          9        46
  3         29        75
  4         19        94
  5          9       103
  6         19       122
  7         20       142
  8         20       162
  9        19       181
10  -84545454 -8978484



it also screwed up my game total..../:
That was my bad, but like I said it's better to not do it that way. I missed the game score and since it is using i also it skips counting scores[0] and the same thing for scores[] in the second column.

Since arrays start at 0, when looping through them it is best to stick with that and only adjust the variable where you need to, like with getting the first column to start at 1 instead of 0.

so which way should I do it.../: cuz it's still not working..
Is it not working like this?
1
2
3
4
5
6
7
8
9
for (int i = 0; i < SIZE; i++)  // < not <=
{
   game += scores[i];

   //column data
   cout << setw(3) << right << i+1;
   cout << setw(11) << right << scores[i];
   cout << setw(10) << right << game << endl;
}

Oh shit I had to take out the <= in the for loop and just use < wow thank you...!! You are the smartest man ALIVE...!!! hahaha..! I was going insane on that one...good stuff!
is there no way I can contact you somehow admkrk for I have other codes too that I'm working on that I'm almost there on just have 2 or 3 errors here in there that I cant track down..
You can PM me and I would be glad to help you figure out how to find the errors.
ok I sure will...(=
Topic archived. No new replies allowed.