How to get the highest number between other numbers - C - c

I've been trying to do a exercise that I have to input a name and a score to 16 candidates. Then the program should return who's the winner (name and score). I'm new into programming and I'm struggling with this program. In my mind I have to get the highest number between these 16 inputs but i have no clue how to do this, that's the part that broke me. Tried a lot of things and nothing worked.
for(candidates = 16; candidates >= 0; candidates--){
printf("\n******************COMPETITION MISS EXTREMA******************\n");
printf("Input candidate name: ");
scanf("%s", name);
printf("Input candidate score:e %s: ", name);
scanf("%f", &score);
printf("Candidate: %s Score: %.2f", name, score);
}
return 0;
}

Create a maximumScore variable and each time you input a score, compare the score with the maximumScore. If the score is greater than maximumScore, update maximumScore with score and continue the process until you complete the execution of for loop.
Finally print the maximumScore to get the maximum number.
float maximumScore = 0.0;
for(candidates = 16; candidates >= 0; candidates--){
printf("\n******************COMPETITION MISS EXTREMA******************\n");
printf("Input candidate name: ");
scanf("%s", name);
printf("Input candidate score:e %s: ", name);
scanf("%f", &score);
printf("Candidate: %s Score: %.2f", name, score);
if (maximumScore < score) {
maximumScore = score;
}
}
printf("Maximum Score: %f", maximumScore);
return 0;
}

Related

The input that i give to my %f does not save

The gradeOne and secondGrade does not save the number that i give. So i cant have the average , because, always end in a division of (0 + 0)/2, can anyone help me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int students;
char name[50];
double gradeOne, secondGrade, classAverage = 0, average = 0;
for(students = 1;students <= 15; students++){
printf("Tell me Your name: \n");
fflush(stdin);
scanf("%[^\n]s", &name);
//saves name
printf("Tell me your first grade: \n");
scanf("%f", &gradeOne);
printf("Tell me your second grade: \n");
scanf("%f", &secondGrade);
//saves grade
printf("\n========================================\n");
printf("Student: %s\n", name);
printf("First Grade: %3.2f \nSecond Grade: %3.2f \n", gradeOne, secondGrade);
printf("%f", average);
printf("\n========================================\n");
average = (gradeOne + secondGrade)/2;
//creates average
printf("Average of the %d student: %3.2f",students, average);
classAverage += average;
//creates class average
}
classAverage = classAverage / (students - 1);
printf("The class average was: %3.2f", classAverage);
return 0;
}
%f in scanf() is for reading float. You should use %lf to read double.
Note that you should use %f in printf() for printing double. Newer specification allows %lf for printf(), but %f should be better for compatibility.

I want to create a menu that shows 1.Client ID, 2.Property details,3.Exit

`#include<stdio.h>
#include<conio.h>
void main()
{
char L,F,H;
float length,breadth,CID,Aoc;/*Pte*/
float Cost_per_sqft,Total_cost, GST;
int dicnt,age,ch;
char Pte;
printf("Enter the Client ID\n");
scanf("%f", &CID);
printf("Enter the age of client\n");
scanf("%d", &age);
if (age >=60)
{
printf("The client is eligible for a discount\n");
}
else if (age<60)
{
printf("The client is not eligible for a discount\n");
}
{ printf("Select Porperty type\nF=Flat\nL=Land\nH=House\n");
scanf(" %c", &Pte);
printf("Enter length\n");
scanf("%f", &length);
printf("Enter breadth\n");
scanf("%f", &breadth);
if (Pte=F)
{
Cost_per_sqft = 5000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
else if (Pte=L)
{
Cost_per_sqft = 6000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
else if (Pte=H);
{
Cost_per_sqft = 9000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
printf("Please select the menu option\n");
printf("1.Client ID\n");
printf("2.Property details\n");
printf("3.Exit\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Client ID = %f\n", CID);
printf("Age of client = %d", age);
break;
case 2:
printf("Property type = %c\n", Pte);
printf("Cost per square feet = %f\n", Cost_per_sqft);
Total_cost = (length*breadth*Cost_per_sqft);
printf("Total cost = %f", Total_cost);
break;
case 3:
;//add content
break;
}
}
}
`
The program is taking value of cost per sqft of House which is 9000, even if I enter Flat or Land variables. another problem is the total cost of the property. instead of taking the desired property types cost per sqft, I believe its multiplying cost per sqft for all values F,L,H. and in the property details menu, the type of property doesn't show either.
I would suggest you to use commenting on your code it'll be easy to find the bug, even for you.
else if (Pte=H);
{
Cost_per_sqft = 9000.0;
printf("Cost per sqft = %f\n", Cost_per_sqft);
}
Try removing the termination operator (';') from the else if statement.
Also use '==' instead of '=' in conditional statements.
eg.
in if (Pte=F) use if (Pte=='F') instead
That should just help you make your code run.

I'm trying to identify the number of student entered in do-while , and display the number out-side the loop

I created the program that takes an input from the user,and display the number of student have been entered..
I use do-while loop because at the end of the program will ask the use "will you want to continue ?" if the user put y, it will loop again to get the input as well. I tried to make an array for it, but it didn't work well for displaying the number of student and the average grade as well...
#include<stdio.h>
int main ()
{
int student_id, test1,test2, final_mark;
float total_up;
char answer;
do {
printf("\nProgram to Calculate Student Grade\n");
printf("Insert the student ID: ");
scanf("%d",&student_id);
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d",&test1);
if (test1 >25)
{
printf("The marks is greater than 25.Please re-insert it again.");
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d",&test1);
}
printf("\nInsert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
if (test2 >25)
{
printf("The marks is greater than 25.Please re-insert it again.");
printf("\nInsert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
}
printf("\nInsert marks for Final Exam: (total of 50): ");
scanf(" %d",&final_mark);
if ( final_mark > 50 )
{
printf("\nThe Final Exam is greater than 50. Please re-insert it again.");
printf("\nInsert marks for Final Exam: (total of 50) ");
scanf("%d",&final_mark);
}
total_up = test1 + test2 + final_mark;
printf("Total Marks: %.2f",total_up);
if (total_up >=80)
{
printf("\nGrade is A\n");
}
else if (total_up >=60)
{
printf("\nGrade is B\n");
}
else if (total_up >=50)
{
printf("\nGrade is C\n");
}
else if (total_up >=40)
{
printf("\nGrade is D\n");
}
else if (total_up <40)
{
printf("\nGrade is F\n");
}
printf("Do you want to continue? ");
scanf(" %c",&answer);
}while (answer == 'y' || answer == 'Y');
printf("\nTotal number of students entered is %d ", student_id);
total_up = total_up / 2;
printf("\nTotal number of students entered is %.2f ",total_up);
return 0;
}
let say the input will be :
Program to Calculate Student Grade
Insert the student ID : 123
Insert marks for Test 1 ( total of 25 ): 15
Insert marks for Test 2 ( total of 25 ): 20
Insert marks for Final Exam ( total of 50 ): 42
Total Marks : 77.00
Grade is B
Do you want to continue ? y
Program to Calculate Student Grade
Insert the student ID : 456
Insert marks for Test 1 ( total of 25 ): 21
Insert marks for Test 2 ( total of 25 ): 23
Insert marks for Final Exam ( total of 50 ): 47
Total Marks : 91.00
Grade is A
Do you want to continue ? n
So, after the do-while terminates..
What I expect is
Total number of students entered is 2
Total number of students entered is 84.00
Average grade is A
I tried an array for student and the test, but it didn't work probably..
Any Idea about it?
For counting student and calculating subtotal, take two extra variables and initialize them to 0 outside the loop. Then, inside the loop, increment the student_count with each iteration of the loop and also add the total marks of current student with the variable of calculating subtotal. Finally, it is easy to calculate average. Just divide subtotal by the student count.
Also, for calculating grade, you can keep it inside another function, as you have to do it twice, once inside the loop and again outside the loop.
#include <stdio.h>
void showGrade(float marks)
{
if (marks >= 80) {
printf("Grade is A\n");
}
else if (marks >= 60) {
printf("Grade is B\n");
}
else if (marks >= 50) {
printf("Grade is C\n");
}
else if (marks >= 40) {
printf("Grade is D\n");
}
else if (marks < 40) {
printf("Grade is F\n");
}
}
int main()
{
int student_id, test1, test2, final_mark, student_count = 0;
float total_up, grand_total = 0, average;
char answer;
do {
student_count++;
printf("\nProgram to Calculate Student Grade\n");
printf("Insert the student ID: ");
scanf("%d", &student_id);
printf("\nInsert marks for Test 1 (total of 25 ): ");
scanf(" %d", &test1);
if (test1 > 25) {
printf("The marks is greater than 25. Please insert it again.");
printf("\nInsert marks for Test 1 (total of 25): ");
scanf(" %d", &test1);
}
printf("Insert marks for Test 2 (total of 25 ): ");
scanf(" %d",&test2);
if (test2 > 25) {
printf("The marks is greater than 25. Please insert it again.");
printf("\nInsert marks for Test 2 (total of 25): ");
scanf(" %d", &test2);
}
printf("Insert marks for Final Exam: (total of 50): ");
scanf(" %d", &final_mark);
if (final_mark > 50) {
printf("\nThe Final Exam is greater than 50. Please insert it again.");
printf("\nInsert marks for Final Exam: (total of 50): ");
scanf("%d", &final_mark);
}
total_up = test1 + test2 + final_mark;
grand_total += total_up;
printf("\nTotal Marks: %.2f\n", total_up);
showGrade(total_up);
printf("\nDo you want to continue? ");
scanf(" %c", &answer);
} while (answer == 'y' || answer == 'Y');
printf("\nTotal number of students entered is %d ", student_count);
average = grand_total / student_count;
printf("\nAverage total number of students entered is %.2f\n", average);
printf("\nAverage ");
showGrade(average);
return 0;
}
Initialize one variable to 0, for storing count of total number of student and increment it after accepting all data from user. At the end just print that variable as "Total number of students entered".`
int studentCount=0, studentTotal=0;
do {
// code to accept all data
studentTotal = studentTotal + total_up;
studentCount++;
}while(answer == 'y' || answer == 'Y');
printf("\n Total number of students entered is %d ", studentCount);
float studentAverage = studentTotal / studentCount;
if (studentAverage >=80)
{
printf("\n Average grade is A\n");
}
else if (studentAverage >=60)
{
printf("\n Average grade is B\n");
}
else if (studentAverage >=50)
{
printf("\n Average grade is C\n");
}
else if (studentAverage >=40)
{
printf("\n Average grade is D\n");
}
else if (studentAverage <40)
{
printf("\n Average grade is F\n");
}

GPA scores program

I am supposed to write a program where the user is asked how many students there are in a class. Then, it asks for the GPA of each of those students. In the end, it is supposed to display number of students with each GPA score. So far this is what i have, but it doesn't seem to count properly.
#include <stdio.h>
int main(void){
int cnt1, cnt2, cnt3, cnt4, student, numofsdts, GPA, GPAFreq[4];
printf("Enter number of students: ");
scanf("%d", &numofsdts);
student = 1;
while(student <= numofsdts){
printf("GPA of student number %d: ", student);
scanf("%d", &GPA);
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
}
else{
student++;
}
if(student == numofsdts + 1)
break;
if(GPAFreq[1])
cnt1++;
else if(GPAFreq[2])
cnt2++;
else if(GPAFreq[3])
cnt3++;
else if(GPAFreq[4])
cnt4++;
}
printf("GPA 1: %d students \n", cnt1);
printf("GPA 2: %d students \n", cnt2);
printf("GPA 3: %d students \n", cnt3);
printf("GPA 4: %d students \n", cnt4);
}
Set int cnt1 = 0, cnt2 = 0 etc. (they are not nullified by default, just have some garbage [like a rented room not cleaned explicitly...]).
Also:
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
continue; // skip the rest of the loop body
}
Or a slightly cleaner approach (in full):
#include <stdio.h>
int main(void){
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
int numofsdts, GPA;
printf("Enter number of students: ");
scanf("%d", &numofsdts);
students = 0;
while(students <= numofsdts){
printf("GPA of student number %d: ", students + 1);
scanf("%d", &GPA);
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
continue;
}
if(GPA == 1)
cnt1++;
else if(GPA == 2)
cnt2++;
else if(GPA == 3)
cnt3++;
else if(GPA == 4)
cnt4++;
students++;
}
printf("GPA 1: %d students \n", cnt1);
printf("GPA 2: %d students \n", cnt2);
printf("GPA 3: %d students \n", cnt3);
printf("GPA 4: %d students \n", cnt4);
}
There are multiple errors here. The first is that cnt1-4 have to be initialized prior to being added to. The second is that C uses zero indexing, so GPAFreq[4] is not accessing the fourth element of your array (which would be GPAFreq[3].
The third is that your if statement isn't doing what you think it is. It is evaluating the values inside of your array as boolean variables, i.e. 0 is false, anything else is true. A better approach is to do this:
GPAFreq[GPA - 1] += 1;
This will be counting the frequencies in each of the indices of the array. Then to print them, you just access GPAFreq and no longer need the cnt variables.

c programming language : the app wont stop after typing x

i made this program and its working just how i want but it should stop when i type x but it doesn't
can any one tell me why?
and if there is any shortcut or smaller way to type this code?
thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int meat[6];
int i;
int total =0;
int avg;
char what[20];
char end;
int days = 0;
char food[20];
char drink[20];
printf("what thing do u want to calculate the average of?(food/drink)\n");
scanf(" %s", &what);
if(strcmp(what, "food")==0)
{
printf("what sort of food did u ate ?\n");
scanf(" %s", food);
}
if(strcmp(what, "drink")==0)
{
printf("what sort of drink did u drank ?\n");
scanf(" %s", drink);
}
for(i=0; i<6; i++)
{
days++;
if(strcmp(what, "food")==0)
{
printf("how many %s did u ate in day %d\n", food, i+1);
}
else if(strcmp(what, "drink")==0)
{
printf("how many liters of %s did u drank in day %d\n", drink, i+1);
}
scanf(" %d", &meat[i]);
total += meat[i];
printf("if u want to continue type y\n");
printf("type x if u want to finish\n");
scanf(" %c", &end);
if((end = 'y')&&(i<5))
{
continue;
}
else if(end = 'x' && strcmp(what, "drink")==0)
{
avg = total / days;
printf("\nyour average amount of liters of %s you had %d\t the total is %d\n", drink, avg, total);
}
else if(end = 'x' && strcmp(what, "food")==0)
{
avg = total / days;
printf("\nyour average amount of %s you had %d\t the total is %d\n", food, avg, total);
}
break;
}
if(strcmp(what, food)==0)
{
printf("\nyour average amount of %s you had is %d\t the total is %d\n", food, avg, total);
}
else if(strcmp(what, drink)==0)
{
printf("\nyour average amount of liters of %s you had is %d\t the total is %d\n", drink, avg, total);
}
return 0;
}
else if(end = 'x' ...
should be:
else if(end == 'x' ...
You use == to test equality in if statements. You've got this in a couple places in your code, which is inadvertently performing an assignment, not what you want to achieve by comparing if user input is equal to a particular character.
Replace the = with == here:
else if(end = 'x' && strcmp(what, "drink")==0)
here:
else if(end = 'x' && strcmp(what, "food")==0)
and here:
if((end = 'y')&&(i<5))

Resources