I am trying to get the numbers in the array to give me a average out put. here is what I have so for:

#include "stdafx.h"
#include <iostream>
#include <cctype>
using std::cout; 
using std::cin;
using std::endl;
 
 
int main() {
int grade[10];
int count = 0;
char reply = 0;
int sum = 0;
do
{
cout<<endl;
cout<<"enter the grades in whole numbers: ";
cin>>grade[count++];
cout<<"Do you want to enter the next grade (y/n)? ";
cin>>reply;
}
while(count < 10 && std::tolower(reply) == 'y');
if(count == 10);
cout << "maximum number of grades reached." << endl;
double average = 0.0;
for(int i = 0; i < count ; i++);
average += grade[i];
average /= count;
cout<<endl;
cout<<"average grade is " <<average <<
cout<<endl;
return 0;
}

thanks for any help you can provide

Recommended Answers

All 15 Replies

This would have been much better as a reply to one of your previous threads. If it's still the same basic question, then reply to the thread, don't start a new one.

> for(int i = 0; i < count ; i++);
That last ; at the end means this loop does nothing.
Always use { } pairs to make your intent clear, even if you intend to only do one statement inside the loop.

So are you saying that if I take the ; off the end of
>for(int i =0; i< count; i++) it will work???

Also, are you saying you can use {} instead of () in a code and they do the same thing??

Thanks for your comments. Since I had made quite a few changes to the code, I figured it would be better to post a new thread. Sorry if this was a problem.
T

No...and no.

Your for loop should be formatted similar to

for(int i = 0; i < count ; i++) {
average += grade[i];
}

() and {} do not do the same thing.

There are many other problems with this code as well.

I'm not normally in the habit of handing out homework solutions...but you need to see a working example.

This is a working program:

#include <iostream>
using namespace std;

int main() {
  int  grade[10];
  int  count = 0;
  int  sum = 0;
  char reply;

  do {
    cout << endl;
    cout << "Enter the grades in whole numbers: ";
    cin >> grade[count];
    count++;
    cout << "Do you want to enter the next grade (y/n)? ";
    cin >> reply;
  } while(count < 10 && tolower(reply) == 'y');

  if(count == 10) {
    cout << "maximum number of grades reached." << endl;
  }

  double average = 0.0;

  for(int i = 0; i < count ; i++) {
    sum += grade[i];
  }

  average = sum/count;
  cout << endl;
  cout << "Average grade is " << average << endl;
  cout << endl;
  return 0;
}

can u make me a multiplication table using a for loop and arrays in c++???
plsss can u??

>>can u make me a multiplication table using a for loop and arrays in c++???
plsss can u??
Arrays wont be required here.
Try this.

int main() {
	int num;
	cout<<"Enter table no.: ";
	cin>>num;
	if(num>0)
	{
		for (int x=1;x<=20;x++) 
		{
			cout<<num<<"x"<<x<<"="<<num*x<<endl;
		}
	}
	else
	{
		cout<<"Not a valid no."<<endl;
	}
	return 0;
}

were i can down load the turbo c++??

can u make me a multiplication table using a for loop and arrays in c++???
plsss can u??

#include<iostream.h>
#include<conio.h>

int main() 
{
        for (int x=1;x<=10;x++) 
        {
            for(int i=1;i<=5;i++)
            cout<<i<<"*"<<x<<"="<<i*x<<"\t\t";
            cout<<endl;
        }
    system("PAUSE");
    return 0;
}

you can change the variable i value you get 1 to 10 or more multiplication table.

commented: No tags, homework freebie -3
#include<iostream.h>
#include<conio.h>

int main()
{
    float n;
    int count=0;
    float sum=0;
    cout<<"Enter Grade (0 for exit):";
    cin>>n;
    while(n)
    {
               sum=sum + n;
               count++;
               cin>>n;
               }
    float ava=sum/count;
    cout<<"average : "<<ava<<endl;
    system("PAUSE");
    return 0;
}

if you are just calculate average of grade you use this.

Just to point error in your original code,
what this is supposed to do?

if(count == 10);

AFAIK, it should be something like

if(count == 10){
 //do some funny stuffs here
}

You guys know this post is 4 years old right?

#include<iostream.h>
#include<conio.h>

int main()
{
    float n;
    float array[30];
    int count=0;
    float sum=0;
    cout<<"Enter Grade (0 for exit):";
    cin>>n;
    while(n)
    {
               array[++count]=n;
               sum+=n;
               cin>>n;
               }
    for(int i=1;i<=count;i++)
    {
            cout<<array[i]<<" ";
            }
    cout<<endl;
    float ava=sum/count;
    cout<<ava<<endl;
    system("PAUSE");
    return 0;
}
#include<iostream.h>
#include<conio.h>

int main()
{
    float n;
    float array[30];
    int count=0;
    float sum=0;
    cout<<"Enter Grade (0 for exit):";
    cin>>n;
    while(n)
    {
               array[++count]=n;
               sum+=n;
               cin>>n;
               }
    for(int i=1;i<=count;i++)
    {
            cout<<array[i]<<" ";
            }
    cout<<endl;
    float ava=sum/count;
    cout<<ava<<endl;
    system("PAUSE");
    return 0;
}
commented: quit spamming a dead thread already.... +0

i m using tc and in c++ the compiler shows error in loop asking to use inline . what this means?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.