I've made this program below to calculate the average mark of a student.
Everything works well until I use -1, it is supposed to stop the program as is display the average of all students that have been entered, say Goodbye! and then terminate.
I think my calculations might be wrong though because it is printing the wrong result for the average marks of the students.
Thanks in Advance.
#include <stdio.h>
int main(void)
{
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int exam_mark);
int i, a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark;
float average_mark = 0.0;
do
{
for (i = 0; i < 2; i++)
{
printf("Enter assignment 1 mark (-1 to quit): ");
scanf("%d", &a_mark1);
if(a_mark1 == -1)
{
average_mark += final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark);
if ((average_mark > 1 ) && (average_mark < 100 ))
{
printf("The average student mark is %.2f%% \n", average_mark);
}
printf("Goodbye! \n");
return 0;
}
printf("Enter assignment 2 mark: ");
scanf("%d", &a_mark2);
printf("Enter laboratory mark: ");
scanf("%d", &lab_mark);
printf("Enter quiz mark: ");
scanf("%d", &quiz_mark);
printf("Enter exam mark: ");
scanf("%d", &exam_mark);
printf("Student %d final mark: %.2f \n", i + 1, final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark));
}
}
while(a_mark1 != -1);
return 0;
}
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int exam_mark)
{
float final_mark = a_mark1 * 0.1 + a_mark2 * 0.15 + lab_mark * 0.15 + quiz_mark * 0.1 + exam_mark * 0.5;
return final_mark;
}
I think you need to rethink your logic a little bit. Why not use a while loop to control the flow. Then you can bail out of the program immediately if user inputs -1 right away. You should use an array to store the averages for each student, then you can loop through and find the class average as well.
-Your float final_mark function seems a little sketchy without any parenthesis.
-You should put your function prototype outside of main as well. See below changes.
#include <stdio.h>
#define MAX_STUDENTS 10 //define what the max number of students is
float final_mark(int mark1, int mark2, int lab, int quiz, int exam);
int main()
{
int i = 0, mark1 = 0, mark2 = 0,
lab = 0, quiz = 0, exam = 0;
int num_students;
float students_avg[MAX_STUDENTS] = {0}; //array to hold averages for students
float average = 0;
while (i < MAX_STUDENTS) {
printf("Enter assignment 1 mark (enter -1 to quit):\n");
scanf("%d", &mark1);
if (mark1 == -1)
break; //no more students, break out of while loop
printf("Enter assignment 2 mark: ");
scanf("%d", &mark2);
printf("Enter laboratory mark: ");
scanf("%d", &lab);
printf("Enter quiz mark: ");
scanf("%d", &quiz);
printf("Enter exam mark: ");
scanf("%d", &exam);
average = final_mark(mark1, mark2, lab, quiz, exam);
students_avg[i] = average; //add this average to array
printf("Student # %d average was %.2f\n", i, students_avg[i]);//debug info
i++;
}
num_students = i; //how many students grades did we read?
average = 0; //reset to 0 so we can use below
for (i = 0; i < num_students; i++)
average += students_avg[i];
if (num_students > 0)
printf("Class average is %.2f\n", average/num_students);
else
printf("Goodbye!\n");
return 0;
}
float final_mark(int mark1, int mark2, int lab, int quiz, int exam)
{
//we can just return the calculation
return ((mark1 * 0.1) + (mark2 * 0.15) + (lab * 0.15) + (quiz * 0.1) + (exam * 0.5));
}
Just erase two lines above printf("GOODBYE! \n"); you will get what you want.
maybe you have to initialized mark s variables to 0. and a_mark1 to 0 too if the user enter -1
Related
# include <stdio.h>
# include <stdlib.h>
int main()
{
int n, tg=0;
printf("Enter the number of subjects : ");
scanf("%d", &n);
while (n>0)
{
int grade;
printf("Enter the marks : ");
scanf("%d", &grade);
n--;
tg+=grade;
}
float avg;
avg = (tg/n);
printf("\nYour Average Grade is %f\n", avg);
return 0;
}
/* It is supposed to show the average grade but it is not displaying any result, but displays this instead
"Process returned -1073741676 (0xC0000094) execution time: 6.532 s"
After the while loop
while (n>0)
{
//...
n--;
}
the variable n is equal tp 0. So this statement
avg = (tg/n);
throws exception dividing by zero.
You could change the while loop for a for loop like for example
int n = 0;
//...
for ( int i = 0; i < n; i++ )
//..
and then after the loop
avg = n > 0 ? ( float )tg/n : 0.0f;
I'm new at this and having some trouble. I'm trying to find the average of the grades that are inputed by the user but I realized that if you use a decimal in any of the grades, it's just being calculated as if they are whole numbers.
#include <stdio.h>
int main(void)
{
unsigned int counter;
float grade;
int total;
float average;
int number;
total = 0;
counter = 1;
printf("Number of scores to enter:\t");
scanf("%d", &number);
printf("\n");
while (counter <= number) {
printf("%s%d%s", "Enter the score for Lab ", counter, ":\t");
scanf("%f", &grade);
total = total + grade;
counter = counter + 1;
}
printf("\n");
average = (float) total / number;
printf("Average lab score: %.1f\n", average);
if (grade>=90) {
puts("Letter grade: A");
}
else if (grade>=80) {
puts("Letter grade: B");
}
else if (grade>=70) {
puts("Letter grade: C");
}
else if (grade>=60) {
puts("Letter grade: D");
}
else {
puts("Letter grade: F");
}
return 0;
}
You are capturing scanf("%f", &grade); as a float and then calculating total = total + grade;.
You have defined int total;. You would need to define it as float total;.
You are moving a float variable into an integer which is truncating the decimals you had previously entered.
There's no need to ask up front how many data points will be entered. Indeed, that is an anti-pattern. Just do something like:
#include <stdio.h>
int
main(void)
{
unsigned int count = 0;
float grade;
float total = 0.0;
float average;
while( scanf("%f", &grade) == 1 ) {
total += grade;
count += 1;
}
average = total / (float) count;
printf("Average lab score: %.1f\n", average);
fputs("Letter grade: ", stdout);
putchar( average >= 90.0 ? 'A' : average >= 80.0 ? 'B' :
average >= 70.0 ? 'C' : average >= 60.0 ? 'D' : 'F');
putchar('\n');
return average >= 60.0;
}
$ echo 78.2 96.5 80 | ./a.out
Average lab score: 84.9
Letter grade: B
Key points: total needs to be a float type. You must check the value returned by scanf. Always. Probably you want to handle bad input more cleanly that this does. This just throws away all data after an error and computes the average based on whatever data was entered prior to the error. A cleaner solution would abort with an error message. Exercise left for the reader.
A reasonable argument can be made that this is an abuse of the ternary operator; however you want to refactor it, don't repeat yourself by hardcoding the string "Letter grade: " multiple times.
Rather than abusing the ternary operator as above, you may prefer something like:
#include <stdio.h>
int
main(void)
{
unsigned int count = 0;
float grade;
float total = 0.0;
float average;
while( scanf("%f", &grade) == 1 ) {
total += grade;
count += 1;
}
average = total / (float) count;
int s = 'A' + (99 - (int)average) / 10;
printf("Average lab score: %.1f\n", average);
printf("Letter grade: %c\n", s > 'D' ? 'F' : s);
return average >= 60.0;
}
I am trying to make a program that will take ask for the grade recieved, the total on the assignment, then average this. They keep doing this till they want to stop, at which point they will input -1. I cannot get it to average correctly, it is always coming out as 0.00, I'm new to C so I am sure it is a simple oversight, Thank you for any help you can offer.
#include <stdio.h>
#include <conio.h>
int main( void )
{
unsigned int counter;
int grade;
int total;
int asavg;
int asavgv;
float average;
total = 0;
counter = 0;
printf("%s", "Enter grade, -1 to end: " );
scanf("%d", &grade );
printf("Enter total possible: ");
scanf("%d", &asavg );
asavgv = grade / asavg;
while ( grade !=-1) {
total = total + asavgv;
counter = counter + 1;
printf ("%s", "Enter grade -1 to end: ");
scanf("%d" , &grade);
printf("Enter total possible: ");
scanf("%d", &asavg );
asavgv = grade / asavg;
}
if ( counter != 0 ) {
average = ( float ) total / counter;
printf("Average is %.2f\n", average );
}
else {
puts("no grades were entered");
}
getch();
}
Change
int asavgv;
to
double asavgv;
and then do
asavgv = grade*1.0 / asavg;
or
asavgv = (double)grade / asavg;
My friend and I are trying to build a program together, but it just doesn't seem to be working. Neither of us have much experience with C, so we just can't spot the issue... Any advice or help would be much appreciated!
Apologies for the slightly awkward lyrics?
[Edit] The problem is that when we input values, we get ridiculous figures like 4586368.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
void main()
{
int room[20] = {};
int i;
int rooms = 0;
char option = 0;
int lights = 0;
int hrsUsed = 0;
int Telly = 0;
int TVWatt =0;
int sumTV;
int TVuse = 0;
int Computer = 0;
int compWatt = 0;
int compUsed = 0;
int compTotal;
int kwH_lights;
int fridge = 0;
int washLoad = 0;
int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
int showeruse = 0;
int total_kWh;
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input average wattage of lights");
scanf_s("%d", &lights);
lights=lights/1000;
printf("input number of hours use/day (average)");
scanf_s("%d", &hrsUsed);
kwH_lights=((lights*hrsUsed)*365);
printf("input number of TVs");
scanf_s("%d", &Telly);
printf("input average wattage");
scanf_s("%d", &TVWatt);
printf("input average use a day");
scanf_s("%d", &TVuse);
sumTV=((Telly*(TVWatt/1000))*TVuse)*365;
}
printf("Input number of fridge/freezer");
scanf_s("%d",&fridge);
fridge=(fridge*2)*365;
printf("input number of Computers and/or video game consoles in the house");
scanf_s("%d", &Computer);
for(i=0;i<Computer;i++) {
printf("input wattage");
scanf_s("%d", &compWatt);
printf("input average hrs used/day");
scanf_s("%d", &compUsed);
compTotal=((compWatt/1000)*compUsed)*365;
}
printf("Input average number of washing machine loads /day");
scanf_s("%d",&washLoad);
washLoad=washLoad*365;
printf("Input average number of clothes dryer loads/day");
scanf_s("%d",&dryerLoad);
dryerLoad=(dryerLoad*3)*365;
printf("Input average number of dishwasher loads/day");
scanf_s("%d",&dishLoad);
dishLoad=(dishLoad*1.5)*365;
printf("Input average cooking load/day");
scanf_s("%d",&cookLoad);
cookLoad=(cookLoad*7)*365;
printf("Input average hrs/day of shower usage");
scanf_s("%d",&showeruse);
showeruse=(showeruse*7)*365;
total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
printf("Total= %d", &total_kWh);
}
You should change this:
printf("Total= %d", &total_kWh);
to that:
printf("Total= %d", total_kWh);
Same is true for all your other integer variables.
There were quite a few mistakes in your code:
you printed the memory-address instead of result value (don't use & with printf if your variable is a plain int)
the computer for-loop had no curly brackets (so only the printf statement was looped)
results were not summed up (in all loops you've just overwritten your inputs from the last loop)
the rooms[] Array was never used - a few other variables also (possible error source, if you wanted to use them and just forgot it)
the result from a multiplication with 1.5 will hold a double value - you should cast that back to int (dishLoad)
The bold mistake is probably that one, why your values were wrong...
Also notice: The 'average number of washing machine loads/clothes dryer loads/ dishwasher loads' should better be asked by week or month... Or should hold Floating Point values: Because everyone I know don't use the washing machine and clothes dryer every day multiple times. So now you can't enter something like once a week (which would be an factor of 0.14, but is not enterable cause all values are stored as int).
Here Comes the code with everything fixed, I could found:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main(int argc, char** argv){
int i = 0;
int rooms = 0;
int lights = 0;
int hrsUsed = 0;
int Telly = 0;
int TVWatt =0;
int sumTV = 0;
int TVuse = 0;
int Computer = 0;
int compWatt = 0;
int compUsed = 0;
int compTotal= 0;
int kwH_lights = 0;
int fridge = 0;
int washLoad = 0;
int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
int showeruse = 0;
int total_kWh = 0;
printf("Enter number of rooms: ");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++){
printf("A few questions about room %d\n", i+1);
printf("input average wattage of lights: ");
scanf_s("%d", &lights);
lights+=lights/1000;
printf("input number of hours use/day (average): ");
scanf_s("%d", &hrsUsed);
kwH_lights+=((lights*hrsUsed)*365);
printf("input number of TVs: ");
scanf_s("%d", &Telly);
printf("input average wattage: ");
scanf_s("%d", &TVWatt);
printf("input average use a day: ");
scanf_s("%d", &TVuse);
sumTV+=((Telly*(TVWatt/1000))*TVuse)*365;
}
printf("Input number of fridge/freezer: ");
scanf_s("%d",&fridge);
fridge=(fridge*2)*365;
printf("input number of Computers and/or video game consoles in the house: ");
scanf_s("%d", &Computer);
for(i=0;i<Computer;i++){
printf("A few questions about computer %d\n", i+1);
printf("input wattage: ");
scanf_s("%d", &compWatt);
printf("input average hrs used/day: ");
scanf_s("%d", &compUsed);
compTotal += ((compWatt/1000)*compUsed)*365;
}
printf("Input average number of washing machine loads/day: ");
scanf_s("%d",&washLoad);
washLoad=washLoad*365;
printf("Input average number of clothes dryer loads/day: ");
scanf_s("%d",&dryerLoad);
dryerLoad=(dryerLoad*3)*365;
printf("Input average number of dishwasher loads/day: ");
scanf_s("%d",&dishLoad);
dishLoad=(int)((dishLoad*1.5)*365);
printf("Input average cooking load/day: ");
scanf_s("%d",&cookLoad);
cookLoad=(cookLoad*7)*365;
printf("Input average hrs/day of shower usage: ");
scanf_s("%d",&showeruse);
showeruse=(showeruse*7)*365;
total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
printf("Total= %d\n", total_kWh);
system("Pause");
return 0;
}
I hope it helps you out - if you got any questions left, feel free to ask.
My first step would be to correct the second for loop { } ... fix this and ask again.
[EDIT]
your calculations with usages of int values divided by other ints (compwatt / 1000) ... are you sure your idea of using int is correct?
or:
cookLoad=(cookLoad*7)*365;
why multiplying with 7 AND 365? should not the average / day be multiplied by 365 only?
For more readability of your code, you can employ compound assignment operators as below,
Operator Name Syntax Meaning
-------------------------------------------------
Addition assignment a += b a = a + b
Subtraction assignment a -= b a = a - b
Multiplication assignment a *= b a = a * b
Division assignment a /= b a = a / b
So i'm writing a statistical calculator program, and the first function I started to write was the mean calculator. My issue is that I'm getting extremely large( and wrong) values for the answers.
Please Enter a number of inputs
4
Please enter number 1
1
Please enter number 2
2
Please enter number 3
3
Please enter number 4
4
Statistical Calculator Menu
(1) Mean
(2) Standard Deviation
(3) Range
(4) Restart/Exit
1
3940705125981218000000000000000000.000000
Here is my source code.
const int MAX_DATA=5;
void menu(float numbers[], int amount);
float mean(float numbers[],int amount);
int main()
{
int i, amount;
float numbers[MAX_DATA];
printf("Please Enter a number of inputs \n");
scanf("%d", &amount);
if (amount>MAX_DATA){
printf("You entered too many numbers");
}else{
for (i=1;i<amount+1;i++){
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
menu(numbers,amount);
}
getch();
return 0;
}
void menu(float numbers[],int amount)
{
int input2;
printf("Statistical Calculator Menu");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit");
scanf("%d",&input2);
if(input2==1){
mean(numbers,amount);
}
}
float mean(float numbers[],int amount)
{
int i;
float sum;
float average;
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
average=sum/amount;
printf("%f", average);
return average;
}
Can someone point out the mistake, or explain why this isn't calculating correctly?
You are not initialising sum so it is taking whatever garbage value was last in that place on the stack. Change:
float sum;
To:
float sum = 0;
Another problem you have is:
for(i = 1; i < amount + 1; i++) {
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
Array indexes start at 0, so this should be:
for(i = 0; i < amount; i++) {
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
Apart from what Mike said,
for (i=1;i<amount+1;i++)
{
printf("Please enter number %d\n", i);
scanf("%f",&numbers[i]);
}
float mean(float numbers[],int amount)
{
// ..
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
....
From this, you are not filling numbers[0]. But in the mean calculation, using the value at 0 index.
float sum;
float average;
for (i=0; i<amount;i++){
sum=sum+numbers[i];
}
sum is not initialized in your program.