I am required to create a function that uses arrays and a menu system that displays:
the sum of all numbers entered, the average of numbers entered, and all numbers entered. It will allow the user to enter up to 1000 numbers.
I have the majority of the code working, I just need to figure out how to display all of the numbers a user has entered so far. Would anyone be able to help me with this? Thanks!
I've tried displaying the number entered, but this does not fit the assignment requirements.
Here is the code I have so far:
/*
Title: Array Intro
Author: James Henderson
Desc: a program designed to display the sume, average, and all previous numbers entered of user input numbers
Date: 11/06/19
*/
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction(userInput)
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInput);
//switch statement
while (1)
{
switch (userInput)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput);
//determine sum
number = userInput;
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
Your approach was right, just look how i used userInput. Below code works fine:
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction()
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInt);
//switch statement
while (1)
{
switch (userInt)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput[counter]);
//determine sum
number = userInput[counter];
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
// number entered so far
printf("\n The list of numbers entered so far : \n");
for(int i=0;i<counter;i++){
printf(" %d ",userInput[i]);
}
printf("\n");
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
int main(){
sumFunction();
}
Related
I am a beginner in C programming and I am stuck in my little program .
I just wanna make a list from which I ask users to select a number from that list. Then the program should do what it should be done.
My list is:
Create a table
Max & Min Number Checking
Negative & Positive Number Checking
Ascending Order
Descending order
Exit
I couldn't start correctly! When I first press 1 to create the table, the program keeps looping again and again!!
I want my program to ask me for some values then take my value and draw me a table and then store these values temporary in memory, so that I can execute the rest of commands from my list.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int t[100],n,a,f,s=0,i,max,min,m,l;
{
printf("Welcome to My simple Math Program in C language \n");
printf("1:Create a table\n 2:Max&Min Number Checking\n 3:Negative&Positive Number Checking\n 4:Ascending Order\n 5:Descending ordert\n 6:Exit\n ");
printf("Please Choose a number from the list" );
scanf("%d",&a);
while(a!=6){
switch(a){
case 1:printf("Please Enter The Length Of Your Table ");
scanf("%d",&n);
printf("Please Enter Your Table Elements ");
for(int i=0;i<n;i++){
scanf("%d",&t[i]);
}
for(int i=0;i<n;i++){
printf("%d ",t[i]);
}
break;
case 3:
if(t[i]<0){
printf("This Number is Negative %d",t[i]);
}
else if(t[i]==0){
printf("This Number is nulle %d",t[i]);
}
else
{
printf("This Number is Positive %d",t[i]);
}
;break;
case 6:
break;
}
}
return 0;
}
}
Your scanf() statement is outside your while loop so it is only executed once. You need something like this:
do
{
scanf("%d",&a);
...rest of code
} while (a!=6)
You should print the message and read the option inside the main loop:
#include <stdio.h>
#include <stdlib.h>
int main() {
int t[100], n, a, f, s = 0, i, max, min, m, l;
printf("Welcome to My simple Math Program in C language\n");
for (;;) {
printf("1: Create a table\n"
"2: Max&Min Number Checking\n"
"3: Negative&Positive Number Checking\n"
"4: Ascending Order\n"
"5: Descending ordert\n"
"6: Exit\n");
printf("Please Choose a number from the list: ");
if (scanf("%d", &a) != 1) /* invalid input */
break;
if (a == 6)
break;
switch (a) {
case 1:
printf("Please Enter The Length Of Your Table ");
if (scanf("%d", &n) != 1)
break;
if (n > 100)
n = 100;
printf("Please Enter Your Table Elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &t[i]);
}
for (i = 0; i < n; i++) {
printf("%d ",t[i]);
}
break;
case 3:
i = 0;
if (t[i] < 0) {
printf("This Number is Negative %d\n", t[i]);
} else
if (t[i] == 0) {
printf("This Number is null %d\n", t[i]);
} else {
printf("This Number is Positive %d",t[i]);
}
break;
}
}
return 0;
}
Here is my code in C, I need help with how to ignore the negative numbers in the printf("You entered %d scores.\n", i); and in the average result.Also how to change int average; to float average; because I don’t get the right average when I change it to float.
Here is my code:
int main()
{
int i, score, sum=0, n;
int average;
for(i=0; score>0; i++)
{
printf("Enter score (4-10) :");
scanf("%d", &score);
if(score>0){
sum = sum + score;
}
}
printf("You entered %d scores.\n", i);
average = sum / i;
printf("the average is: %d", average);
}
The required Output:
The program calculates the average of scores you enter.
End with a negative integer.
Enter score (4-10):7
Enter score (4-10):8
Enter score (4-10):9
Enter score (4-10):10
Enter score (4-10):4
Enter score (4-10):4
Enter score (4-10):5
Enter score (4-10):-1
You entered 7 scores.
Average score: 6.71
It seems that the average, sum, and scores should all be decimal values (floats).
That means you have to change the scanf argument as well and the printf argument.
When dividing the integer i into the float sum, there is no need to multiply by 1.0, as long as sum is a float.
#include <stdio.h>
int main()
{
int i;
float score;
float sum = 0;
float average;
for (i = 0; score > 0; i++) {
printf("Enter score (4-10) :");
scanf("%f", &score); // accept decimals in the scores
if (score > 0) {
sum = sum + score;
} else {
break; /// leave the loop here to prevent incrementing i
}
}
printf("You entered %d scores.\n", i);
average = sum / i; // as sum is a float, this division will now work.
printf("the average is: %2.2f", average); // print 2 decimal places as a float
}
int main()
{
int i, score = 1, n;
float sum = 0.0, average; //<-------------changed to float
for(i=0; score>0; i++)
{
printf("Enter score (4-10) :");
scanf("%d", &score);
if(score>0){
sum = sum + score;
}
else{
break;
}
}
printf("You entered %d scores.\n", i);
average = sum / i;
printf("The average is: %f", average); //<--- changed to %f you can use &.2f to print 2 digits after .
return 0;
}
I've been stumped for the past few days trying to modify my current code to be able to input an undetermined number of students.
#include <stdio.h>
int main(void)
{
char StudentName[100];
float ExamValue, Sum, Avg;
int students, exams;
for (students = 0; students < 5; students++)
{
Sum = 0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
for (exams = 0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum / 3.0;
printf("Average for %s is %f\n", StudentName, Avg);
}
return 0;
}
As it is now, I have to manually input the amount of students. Does anyone know how I can modify this code in order to enter an undetermined amount of students? I'm starting to think that it is impossible to do and maintain the integrity of the rest of the code. Any help is greatly appreciated, thanks!
You can do something like while (stillAdding) instead of the for loop, and prompt the user with Enter student name or QUIT to stop, or even Would you like to enter a new student [Y/n]. You'd modify the stillAdding variable accordingly. In short, you leave it up to the user to specify when they want to stop inputting more data.
You can ask for the number of users before the for and then use that number as upper bounds of the for. Something like this:
int students, exams, nr;
printf("Enter Student Number \n");
scanf("%d", &nr);
for (students = 0; students < nr; students++)
{
//your code
}
You can ask the user whether there are more students per loop:
#include <stdio.h>
int main(void)
{
char StudentName[100];
float ExamValue, Sum, Avg;
int students, exams;
char stop;
for (;;)
{
Sum = 0.0;
printf("Enter Student Name \n");
scanf(" %s", StudentName);
for (exams = 0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum / 3.0;
printf("Average for %s is %f\n", StudentName, Avg);
puts("More students?(Y/N)");
scanf("%*[^yYnN]%c%*[^\n]%*c", &stop); // read one of 'y', 'Y', 'n', 'N', then discard that line, including '\n'.
if (stop == 'N' || stop == 'n')
break;
}
return 0;
}
You can prompt the user to supply the number of inputs. Once the user tells you how many inputs will be given, then you can simply use a for loop to read that many inputs
In my program I'm messing around with, it simply asks for how many tests one has written and then returns an average. However I've modified it a bit so that it asks if the marks entered are correct.
Problem 1: It doesn't let you input your marks for all your tests
Problem 2: If the marks are wrong it starts over but keep the previous inputs in it's memory? How do I fix the?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//int variables for grade
unsigned int counter; //number of grades to be entered next
int grade;
int total;
float average;
// user input
int userInput; // amount of tests
int yesNo;
//amount of test passed
unsigned int pass = 0;
unsigned int fail = 0;
int doCount = 1;
//unsigned int test;
//---------------------------------------------------------------------------------------------------//
//standards for program to abide to
total = 0; //Total amount of test to be set to zero, until while statement
counter = 1; //Loop counter to start from one
//---------------------------------------------------------------------------------------------------//
printf ("Please enter amount of test you've written so far: ");
scanf ("%d", &userInput);
//printf ("%d", userInput);
//---------------------------------------------------------------------------------------------------//
do {
//Body of calculations of program
for(counter = 0; counter <= userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
printf ("Please enter percentage mark: "); //prompt for test mark
scanf("%d", &grade);
total = total + grade;
counter = counter + 1;
if (grade >= 40) { //if statement for pass or fail
pass = pass + 1;
} else {
fail = fail + 1;
}
}//end of for loop
printf ("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct
scanf ("%d", &yesNo);
if (yesNo == 2) {
} else {
average = ((float)total / userInput); //Getting average for tests so far
//if statement to clarify if you're passing
if (average < 40) {
printf ("\nYou are below sub minimum!\n");
printf ("Your overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
} else if (average >= 75){
printf ("\nYou have a distinction agregate!\n");
printf ("Your overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
} else {
printf ("\nYour overall average is: %.2f %\n", average);
printf ("Passed: %d\n", pass);
printf ("Failed: %d", fail);
}
doCount = 2;
}
} while (doCount == 1);
average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//
getch ();
return 0;
}
In your do while loop, when you come around for your second pass you need to reset your variables. Specifically the total variable should be reset to zero. You do it for the first time outside the do while loop but once it's in the loop for the second pass it doesn't get reset to 0.
As for not reading all test inputs, if it asks for 9 but you need 10 then it likely is a problem with the for loop. I typically use counter++ and not ++counter as it increments the counter after the operation and not before the operation. That may or may not be the reason as I did not run your code, but it is worth looking at.
I've edited your code and commented the changes:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
//int variables for grade
unsigned int counter; //number of grades to be entered next
int grade;
int total;
float average;
// user input
int userInput; // amount of tests
int yesNo;
//amount of test passed
unsigned int pass = 0;
unsigned int fail = 0;
int doCount = 1;
//unsigned int test;
//---------------------------------------------------------------------------------------------------//
//standards for program to abide to
total = 0; //Total amount of test to be set to zero, until while statement
counter = 0; //Loop counter to start from zero, It's always better to start from zero
//---------------------------------------------------------------------------------------------------//
printf("Please enter amount of test you've written so far: ");
scanf("%d", &userInput);
//printf ("%d", userInput);
//---------------------------------------------------------------------------------------------------//
do {
//Body of calculations of program
total = 0; //You need to reset total pass and fail
pass = 0;
fail = 0;
for (counter = 0; counter < userInput; ++counter) { //for loop that correlates to userInput for amount of passes and test marks
printf("Please enter percentage mark: "); //prompt for test mark
scanf("%d", &grade);
total = total + grade;
//counter = counter + 1; You DON't need that
if (grade >= 40) { //if statement for pass or fail
pass = pass + 1;
}
else {
fail = fail + 1;
}
}//end of for loop
printf("Are the grades entered correct? (1 = yes, 2 = no): "); // user input for yesNo - are inputs correct
scanf("%d", &yesNo);
if (yesNo == 2) {
}
else {
average = ((float)total / userInput); //Getting average for tests so far
//if statement to clarify if you're passing
if (average < 40) {
printf("\nYou are below sub minimum!\n");
printf("Your overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
else if (average >= 75) {
printf("\nYou have a distinction agregate!\n");
printf("Your overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
else {
printf("\nYour overall average is: %.2f %\n", average);
printf("Passed: %d\n", pass);
printf("Failed: %d", fail);
}
doCount = 2;
}
} while (doCount == 1);
average = ((float)total / userInput); //Getting average for tests so far
//---------------------------------------------------------------------------------------------------//
getch();
return 0;
}
I'm writing the simplest game in C - Number guessing game. The game itself works good. Yaay me. The problem is that I don't know how to start it over. See code below:
int main()
{
int number, innum, times = 0;
char playAgain;
srand((unsigned)time(NULL));
number = 5;//rand() % 1000;
for(;;)
{
while(innum != number)
{
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number)
{
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf("%c", &playAgain);
if(playAgain == 'n')
break;
}
return 0;
}
The first problem is that it prints "Do you want to play again?" two times. Why is that? And the other problem is, how do I get the game to start again?
Thanks in advance.
This should work for you:
(What i did? Added a space by the scanf and put the declaration of number, times and innum in the for loop)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, innum, times;
char playAgain;
srand((unsigned)time(NULL));
for(;;) {
/*Declare the variables here*/
number = 5; //rand() % 1000;
innum = 0;
times = 0;
while(innum != number) {
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number) {
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf(" %c", &playAgain);
//^Added space here to 'eat' any new line in the buffer
if(playAgain == 'n')
break;
}
return 0;
}
possible output:
Enter a number: 2
The entered number is too small!
Enter a number: 6
The entered number is too big!
Enter a number: 5
Congrats you guessed right!
It took you 3 tries
Do you want to play again?y
Enter a number: 3
The entered number is too small!
Enter a number: 5
Congrats you guessed right!
It took you 2 tries
Do you want to play again?n