Question

Write a program in C: Using atoi() function to calculate the average of an input series...

Write a program in C:

Using atoi() function to calculate the average of an input series of number. User enter q or Q it will exit program and return average of series of number above.

Example output:

Enter the first number or Q to quit: 5

Enter the next number or Q to quit: 8

Enter the next number or Q to quit: -1

Enter the next number or Q to quit: 58

Enter the next number or Q to quit: q

Average: 17.5

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

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(void)

{

int count = 0;

double sum = 0;

char input[] = "";

do

{

if(count == 0)

{

printf("\nEnter the first number or Q to quit: ");

}

else

{

printf("Enter the next number or Q to quit: ");

}

scanf("%s", input);

if(strcmp(input, "q") == 0 || strcmp(input, "Q") == 0)

break;

int num = atoi(input);

sum += num;

count++;

}while(strcmp(input, "q") != 0 || strcmp(input, "Q") != 0);

double average = (double)(sum / count);

printf("Average: %0.1f", average);

return 0;

}

********************************************************************* SCREENSHOT ******************************************************

Add a comment
Know the answer?
Add Answer to:
Write a program in C: Using atoi() function to calculate the average of an input series...
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
  • Write a C++ program that will calculate average of squares up to n. For example, if...

    Write a C++ program that will calculate average of squares up to n. For example, if user enters 3 the function should display 1 + 4 + 9/ 3 = 4.6667. Requirements Created function to receive 1 parameter and no return parameter. Called the new function from main function inside a while loop. Calculated the sum of squares and average correctly in the new function using loop with no library functions. Sample Output ~~Enter a number ONLY between 1 and...

  • Write a c++ program to calculate the approximate value of pi using this series. The program...

    Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop...

  • need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that...

    need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that first prompts the user for an upper limit: Enter the number of entries: 5 € (user enters 5) After the user enters a number, the program should prompt the user to enter that many numbers. For example, if the user enters 5, then the program will ask the user to enter 5 values. Use a do-while loop to add the numbers. o With each...

  • Write a C++ Program. you were required to first create a function called average. The return...

    Write a C++ Program. you were required to first create a function called average. The return type is double and it takes in two parameters, one of type string the other of type double. In the average function write a for loop that PROMPT THE USER to enter in a grade 5 times, keep a sum of all the different grades then return the average of the grades. (sum / 5) Next write a function of type double called lowest...

  • lab software using cygwin part1 part2 1 of 1 1. Write a shell program name it...

    lab software using cygwin part1 part2 1 of 1 1. Write a shell program name it "lab4q1.sh" to compare the two strings: a. Read the first string. b. Read the second string. c. If the strings are empty print "String Empty" and exit using the statement exit 1. d. Compare the two strings using the if and print longer string. e. If the size of both strings is same, then print that two strings are equal (use printf), otherwise, print...

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • In Java #2 JAVA Write a program named salaryCalculator that accepts input from the user that...

    In Java #2 JAVA Write a program named salaryCalculator that accepts input from the user that represents an annual salary for employees. This program should all be written in the main driver program no additional classes are needed. Your program should do the following: Prompt the user for a salary continually until the user enters a sentinel value, a negative number. Keep two running totals the first is the total of all original salaries and the second is a total...

  • Write a program that receives a series of numbers from the user and allows the user...

    Write a program that receives a series of numbers from the user and allows the user to press the enter key to indicate that he or she is finished providing inputs. After the user presses the enter key, the program should print the sum of the numbers and their average.   >>> totalSum =0 >>> count=0 >>> while True:    number=input("Enter a number or press enter to quit:")    if number =="":        break    totalSum += float(number)    count+=1...

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