c++ transformation from for to while and do while loop

So I was supposed to make a program that can calculate the total average of the amount of grades they wish to calculate. The program below works fine. The only problem is that I need to transform it into a while and do while. How do I do that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>

using namespace std;

int main ()
{
	int n, i;
    int num, sum, average;

    cout << "Enter the number of grades: ";
    cin >> n;

    for( i = 0; i < n; ++i )
    {
        cout << i + 1 << ". Enter grade: ";
        cin >> num;
        sum = sum + num;
    }
    
    average = sum / n;
    cout << "Average = " << average;
}
comment out for( i = 0; i < n; ++i ) and start thinking about the while that goes in its place. Declare the variable that increments above the while, keep the same true condition as in the for loop, and increment the variable at the end of the while body.
Last edited on
Hello DigiLei,

What exactly do you want to put into loops?

I could see putting lines 10 - 21 in a do/while loop and you could change the for loop to a while loop.

I generally a format like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>

int main()
{
	char ans{};
	bool cont{ true };

	do
	{
		// Code here.

		std::cout << "Do another Y/N: ";  // <--- Change message to what you want.
		std::cin >> ans;
		ans = std::toupper(ans);

                if (ans == 'N')
                    cont = false;

	} while (cont);

}


Or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>

int main()
{
	char ans{};
	bool cont{ true };

	do
	{
		// Code here.

		std::cout << "Do another Y/N: ";  // <--- Change message to what you want.
		std::cin >> ans;
		ans = std::toupper(ans);

	} while (ans != 'N');
}

Any letter or number other than 'N' will allow the program to continue.

Hope that helps,

Andy
closed account (E0p9LyTq)
Duplicating the execution of your program exactly using a while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
   int n;

   std::cout << "Enter the number of grades: ";
   std::cin >> n;
   
   int i = 0;
   int sum = 0;
   // a while loop might not be executed even one loop
   while (i < n)
   {
      std::cout << i + 1 << ". Enter grade: ";
      int num;
      std::cin >> num;
      sum = sum + num;
      i++;
   }

   int average = sum / n;
   std::cout << "Average = " << average << '\n';
}

Using a do/while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
   int n;

   std::cout << "Enter the number of grades: ";
   std::cin >> n;
   
   int i = 0;
   int sum = 0;
   // a do/while loop is looped at least one time
   do
   {
      std::cout << i + 1 << ". Enter grade: ";
      int num;
      std::cin >> num;
      sum = sum + num;
      i++;
   } while (i < n);

   int average = sum / n;
   std::cout << "Average = " << average << '\n';
}

Redoing the program so you can enter grades without entering the number of grades:
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
#include <iostream>

int main()
{
   std::cout << "Enter the grades (-1 to QUIT):\n";
   
   int i = 0;
   int sum = 0;
   // a while (true) loop never ends
   while (true)
   {
      std::cout << i + 1 << ". Enter grade: ";
      int num;
      std::cin >> num;

      if (num <= -1)
      {
         // terminate the unending loop
         break;
      }

      sum += num;
      i++;
   }

   int average = sum / i;
   std::cout << "Number of grades: " << i << "\tAverage: " << average << '\n';
}

Enter the grades (-1 to QUIT):
1. Enter grade: 2
2. Enter grade: 5
3. Enter grade: 5
4. Enter grade: 4
5. Enter grade: 8
6. Enter grade: 9
7. Enter grade: 2
8. Enter grade: 2
9. Enter grade: -1
Number of grades: 8     Average: 4

I didn't check the input for -1, I checked for any negative integer indicating the user is done inputting grades.
closed account (E0p9LyTq)
make a program that can calculate the total average of the amount of grades

Storing your average in an int doesn't give the best representation of the average, you should use either a float or double.

Using an int truncates any decimal fraction. 4.335 becomes 4.

Integer math can also be a problem if you want a decimal fraction. 6/4 should be 1.5, but it will be 1.
Topic archived. No new replies allowed.