write a program that will find the sum, mean, or mode of a set of integers, or a
ID: 640920 • Letter: W
Question
write a program that will find the sum, mean, or mode of a set of integers, or a count of the total number of odd or even numbers. The first argument to your program will be a string that specifies which operation the user desires: "sum", "mean", "mode", "odd", or "even". You will then use std::cin to read in the user's integers. Return the value 0 from your main function upon successful completion.
Examples:
./homework1 sum
1 2 3 7 8 9 quit
30
./homework1 mode
1 2 3 3 4 5 6 6 6 7 8 9 quit
6
./homework1 mean
-1 -2 -3 -4 stop
-2.5
./homework1 odd
-1 -2 -3 -4 stop
2
Details:
The mean may be a fractional number. For example, the mean of 5 and 6 is 5.5.
If you use values of type 'int' this will be truncated to 5. Use values of type 'float' to obtain the correct average.
//This math will round off:
int sum = 5 + 6;
int total_numbers = 2;
int average = sum / total_numbers;
//This math will not round off because the values are of type 'float' rather than type 'int'.
float sum = 5 + 6;
float total_numbers = 2;
float avg_float = sum / total_numbers;
When checking for an odd or even numbers you can use the modulo operator (%) to see if there is a remainder after dividing by 2. For example, (3 % 2) is 1.
Input:
Your program will accept a single string option as a command line argument (argv[1]).
The option should be "sum", "mean", "mode", "odd", or "even".
Then a series of integers should be read in from std::cin. Stop reading input
when there is a non-integer input, e.g.
while (std::cin >> number) {
...
}
Output:
f the option is sum then print out the sum of all of the numbers and return 0.
If the option is mean then print out the average of all of the numbers and return 0.
If the option is mode then print out the most frequently occurring number (if
two are tied then you may return either) and return 0.
If the option is odd then print out the number of odd numbers numbers and return 0.
If the option is even then print out the number of even numbers numbers and return 0.
THE PROGRAM MUST DO THE FOLLOWING:
Successfully compile and pass any test
Return 1 when given no arguments
Return 2 when given an unexpected option argument
Return 3 when given a proper option but no numbers
Return 0 when given proper arguments
Successfully count the number of odd numbers
Successfully count the number of even numbers
Successfully sum the numbers
Successfully average the numbers
Successfully find the mode
Explanation / Answer
#include <stdio.h>
void main()
{
int numbers[100], i, num,odd=0,even=0,sum=0;
printf("Enter the number of elementsin array ");
scanf("%d", &num);
printf("Enter the elements of the array ");
for (i = 0; i < num; i++)
{
scanf("%d", &numbers[i]);
}
printf("Even numbers in the array are - ");
for (i = 0; i < num; i++)
{
if (numbers[i] % 2 == 0)
{
printf("%d ", numbers[i]);
even++;
}
}
printf(" Odd numbers in the array are -");
for (i = 0; i < num; i++)
{
if (numbers[i] % 2 != 0)
{
printf("%d ", numbers[i]);
odd++;
}
}
for(i=0;i<num;i++){
sum=sum+numbers[i];
}
printf("odd count=%d",odd);
printf("even count=%d",even);
printf("sum=%d",sum);
printf("avergafe=%d",sum/num);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.