Question

i'm having trouble making an else statement that will allow the program to add and output...

i'm having trouble making an else statement that will allow the program to add and output the average the valid numbers inputed into the program.

Here's my output:

Enter Score 1:36

Enter Score 2:-1

Invalid Input

Enter Score 2:89.5

Enter Score 3:42

Enter Score 4:66.3

Enter Score 5:93

You entered: 36.000000, 89.500000, 42.000000, 66.300000, 93.000000

Total Score: 362.800000

Average Score: 72.560000

Max Score: 93.000000

Min Score: 36.000000

Here's my code:

#include <stdio.h>

int main(void) {

double score[5];

double min;

double max;

double total;

double avg;

int i;

max = 0.0;

min = 100.00;

total = 0.0;

for(i=0;i<5;i++) {

printf("Enter Score %d:",i+1);

scanf("%lf",&score[i]);

if(score[i] <0.0 || score[i]>100.00) {

printf("Invalid Input\n");

--i;

}

else{

}

total+=score[i];

if (score[i] > max)

max = score[i];

if (score[i]<min)

min = score[i];

}

avg = total/5.0;

printf("\nYou entered: ");

for(i=0;i<5;i++) {

printf("%lf",score[i]);

if(i<4)

printf(", ");

}

printf("\nTotal Score: %lf",total);

printf("\nAverage Score: %lf",avg);

printf("\nMax Score: %lf",max);

printf("\nMin Score: %lf",min);

printf("\n");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<limits.h>

float sum(float arr[], int n)

{

    float ans = 0;

    int i;

  

    // compute the sum of array

    for( i = 0 ; i < n ; i++ )

        ans += arr[i];

      

   return ans;

}

float average(float arr[], int n)

{

    float sum = 0;

    int i;

  

    // compute the sum of array

    for( i = 0 ; i < n ; i++ )

        sum += arr[i];

      

    // compute average and return

    return sum / (float)n;

}

float lowest(float arr[], int n)

{

    int i;

    float min = (float)INT_MAX;

  

    // traverse the vector

    for( i = 0 ; i < n ; i++ )

        // if the current element is smaller than min

        if( arr[i] < min )

            min = arr[i];

          

    return min;

}

float highest(float arr[], int n)

{

    int i;

    float max = (float)INT_MIN;

  

    // traverse the vector

    for( i = 0 ; i < n ; i++ )

        // if the current element is greater than max

        if( arr[i] > max )

            max = arr[i];

          

    return max;

}

int main()

{

    float arr[5];

    int i;

  

    for( i = 0 ; i < 5 ; i++ )

    {

        // infinite loop

        while(1)

        {

            printf("Enter Score %d: ", i + 1);

          

            // get user input

            scanf("%f", &arr[i]);

          

            // if score is valid

            if( arr[i] >= 0.0 && arr[i] <= 100.0 )

                break;

              

            printf("Invalid Input\n");

        }

    }

  

    printf("You eneterd : ");

  

    for( i = 0 ; i < 5 ; i++ )

        printf("%f ", arr[i]);

  

    printf("\nTotal Score : %f\n", sum(arr, 5));

    printf("Average Score : %f\n", average(arr, 5));

   printf("Max Score : %f\n", highest(arr, 5));

    printf("Min Score : %f", lowest(arr, 5));

    return 0;

}


Sample Output

Enter name Jack Enter votes recieved 52 Enter name Jill Enter votes recieved 41 Enter name: Bob Enter votes recieved 13 Enter

Add a comment
Know the answer?
Add Answer to:
i'm having trouble making an else statement that will allow the program to add and output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • I'm having with my C program whenever i insert numbers it keeps outputting zeros here's the...

    I'm having with my C program whenever i insert numbers it keeps outputting zeros here's the code: #include<stdio.h> int main(void) {    int weeklyhours;    double hourlyrate;    double grosspay;    double netpay;    double federal;    double state;    double FICA;    double Medicare;    grosspay= weeklyhours * hourlyrate;    federal = grosspay * 0.1;    state = grosspay * 0.06;    FICA = grosspay * 0.062;    Medicare = grosspay * 0.0145;    netpay = grosspay - (federal...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ā€˜yā€™ or ā€˜nā€™ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int...

    I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int main() { char gender; float a1, a2, a3, a4, a5; float waistmeasurement, wristmeasurement, hipmeasurement, forarmmeasurement; float B, bodyweight, Bodyfat, Bodyfatpercentage;    printf("This program determines the body fat of a person.Enter your gender (f|F|m|M): "); scanf("%c", &gender); printf("\n");    if(gender=='F' || gender=='f'){ printf("Enter body weight (in pounds): \n"); scanf("%f", &bodyweight); printf("Enter wrist measurement at fullest point (in inches): \n"); scanf("%f", &wristmeasurement); printf("Enter waist measurement at...

  • C code program help. I am trying to find the total parallel resistive load. This is...

    C code program help. I am trying to find the total parallel resistive load. This is what I have so far. It does run, it does ask for numbers. But it doesn't calculate. It just comes out "The parallel is: 0.00". I have tried 3, 4, 5 and 3.2, 4.3, 5.4. Still only shows 0.00. I have tried several combinations of putting different data types in both main.c and the rest of it. Any help or ideas would be helpful....

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • The program has errors. Identify and make the necessary changes to make the program work. Remember...

    The program has errors. Identify and make the necessary changes to make the program work. Remember to follow the rules of functions and arrays. Here is the expected output Total = 20 Average = 15 Area = 150 Area = 70 Enter a value: 10 Value 1 entered = 10 Enter an integer: 20 Value 2 entered = 20 Enter an integer: 30 Value 3 entered = 30 .................................................................. #include <iostream> using namespace std; void total(int, int, int); double average(int...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT