15,741,947 members
See more:
The purpose of this programme is to prompt the user for
input his/her grades for each category
. It will then it find the
grade average of each category and use them to calculate the

What I have tried:

```include<stdio.h>
int main(){
printf("This program will input your homework,quizzes,tests,and final exams and calculate your average.\n");
char input;
int i,n,g;
bool t=true;
int sum=0,sum1=0,sum2=0,sum3=0;
int average;
//Input h for homework,q for quizzes,t for tests and f for final exams.
while(t==true){
scanf("%c",&input);
if(input=="h"){
printf("How many homework grades are to be entered?:");
scanf("%d",n);
for(i=1;i<=n;i++){
scanf("%d",&g);
sum+=g;

}
}
if(input=="q"){
printf("How many quizzes grades are to be entered?:");
scanf("%d",n);
for(i=1;i<=n;i++){
}
}
if(input=="t"){
printf("How many test grades are to be entered?:");
scanf("%d",n);
for(i=1;i<=n;i++){
}
}
if(input=="f"){
printf("How many final grades are to be entered?:");
scanf("%d",n);
for(i=1;i<=n;i++){
}
}
printf("Do you want to input any other grade:");
scanf("%c",input);
else{
t=false;
}
}

}```
Posted
Updated 25-Mar-22 8:13am
v3

## Solution 4

C
```#include <stdio.h>
#include <stdbool.h>
int main()
{
printf("This program will input your homework,quizzes,tests,and final exams and calculate your average.\n");
char input;
int i,n,g;
bool t=true;
int sum=0,sum1=0,sum2=0,sum3=0;
int average;
//Input h for homework,q for quizzes,t for tests and f for final exams.
while (t==true)
{
scanf("%c",&input);
if(input=='h')
{
printf("How many homework grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d",&g);
sum+=g;
}
}
if(input=='q')
{
printf("How many quizzes grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d",&g);
sum1+=g;
}
}
if(input=='t')
{
printf("How many test grades are to be entered?:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d",&g);
sum2+=g;
}
}
if(input=='f')
{
printf("How many final grades are to be entered?:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d", &g);
sum3+=g;
}
}
else
{
t = false;
}
if ( t )
{
char c;
printf("Do you want to input any other grade:");
while ((c = getchar()) !='\n' && c != EOF) {}
}
}```

Then, I made it better (use data structures and functions, Luke!)
C
```#include <stdio.h>
#include <stdbool.h>

{
char kind;
const char * desc;
bool valid;
double average;
};

int main()
{
{
{ 'h', "homework", false, 0.0 },
{ 'q', "quizzes",  false, 0.0 },
{ 't', "tests",  false, 0.0 },
{ 'f', "final exams", false, 0.0 },
};

printf("This program will input your homework,quizzes,tests,and final exams and calculate your average.\n");

while (true)
{
char input;
input = getchar();
int g;
for (g=0; g < GRADES; ++g)
{
break;
}
break;

while ((input = getchar()) != '\n' && input != EOF) {}
}

for (int g=0; g < GRADES; ++g)
{
}

return 0;
}

{
int items;
double average = 0.0;
printf("how many %s grades ? ", desc);
scanf("%d", &items);
for (int i = 0; i < items; ++i)
{
int g;
scanf("%d", &g);
average += g;
}
if ( items )
average /= items;
return average;```

Patrice T 25-Mar-22 20:06pm
+5
CPallini 26-Mar-22 3:38am
Thank you.

## Solution 3

As a beginner, it can be difficult to see where braces line up and where they are mismatch.

Do NOT do this:
```int main(){
if(){
}
else{
}
}
```

```int main()
{
if()
{
}
else
{
}
}
```

It'll make it far easier to see where you messed up your braces. You have at least one extra brace where it doesn't belong.

CPallini 25-Mar-22 13:19pm
5.

## Solution 1

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just saying "I tried" and expecting others to do it for you isn't going to cut it.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

Saloni Choudhary 25-Mar-22 11:51am
I understand that and I do know coding but I am not able to even start. I am still trying but not able to understand how to proceed.
Saloni Choudhary 25-Mar-22 11:56am
I have posted what I tried doing but I am very confused.
OriginalGriff 25-Mar-22 12:06pm
First, indent that properly so it's readable, and replace all the single character variables with descriptive names: cntHomework, cntQuiz, ... so that you are maintaining separate counts. And maintain separate sums as well!

Then the averages are easy to work out: sum / count == average

But tidy that mess up first - and then follow the link I gave you and read the article!
Saloni Choudhary 25-Mar-22 12:08pm
ok
CPallini 25-Mar-22 13:19pm
5.