patterncMinor
Testing numbers using 'switch'
Viewed 0 times
numbersswitchtestingusing
Problem
My task is:
Write a program that reads integers until 0 is entered. After input terminates, the
program should report the total number of even integers (excluding the 0)
entered, the average value of the even integers, the total number of odd integers
entered, and the average value of the odd integers.
But I had to use switch for this...
My code is:
Is this code ok?
Write a program that reads integers until 0 is entered. After input terminates, the
program should report the total number of even integers (excluding the 0)
entered, the average value of the even integers, the total number of odd integers
entered, and the average value of the odd integers.
But I had to use switch for this...
My code is:
#include
int main(void)
{
int oddIndex = 0, evenIndex = 0, evenTotal = 0, evenSum, oddTotal = 0, oddSum;
int userInput;
int oddArray[oddIndex], evenArray[evenIndex];
printf("please enter some numbers: \n");
while (((scanf("%d", &userInput)) == 1) && (userInput != 0))
{
switch (userInput%2)
{
case 0:
evenArray[evenIndex] = userInput;
evenIndex++;
evenTotal++;
evenSum += userInput;
break;
default:
oddArray[oddIndex] = userInput;
oddIndex++;
oddTotal++;
oddSum += userInput;
break;
};
}
if (evenTotal > 0)
printf("even total: %d, even average: %d\n", evenTotal, evenSum/evenTotal);
else
printf("there is no even numbers!\n");
if (oddTotal > 0)
printf("odd total: %d, odd average: %d", oddTotal, oddSum/oddTotal);
else
printf("there is no odd numbers!");
}Is this code ok?
Solution
I haven't been a C guy for years, but a few observations:
You have
I think
I'm not sure what the standard is for C, but I really don't like the multiple declarations on one line. There should be a single declaration per line. Its less confusing that way.
You could also put some error handling around the input. What happens if somebody enters "A"? Your program would crash. I also think your instruction should be a little more clear. What does "please enter some numbers." really mean?
Minor thing, you forgot to indent the line after the if's at the bottom.
I would also move the reporting to a different function and reduce duplication.
Your code could turn out like this:
Please excuse me if I have the syntax wrong, I haven't used C in about 20 years.
You have
int oddArray[oddIndex], evenArray[evenIndex]; declared, but the only time you use it is to add the entered number, and even that will fail because you have basically declared them as size[0], so oddArray[1] will throw an exception.I think
[even|odd]Index and [even|odd]Total can be combined into one variable.I'm not sure what the standard is for C, but I really don't like the multiple declarations on one line. There should be a single declaration per line. Its less confusing that way.
You could also put some error handling around the input. What happens if somebody enters "A"? Your program would crash. I also think your instruction should be a little more clear. What does "please enter some numbers." really mean?
Minor thing, you forgot to indent the line after the if's at the bottom.
I would also move the reporting to a different function and reduce duplication.
Your code could turn out like this:
#include
void DisplayResults(char numberType[], int total, int sum);
int main(void)
{
int oddCount= 0;
int evenCount= 0;
int evenSum = 0;
int oddSum = 0;
int userInput;
printf("Please enter a number followed by . Enter \"0\" to exit: \n");
while (((scanf("%d", &userInput)) == 1) && (userInput != 0))
{
switch (userInput%2)
{
case 0:
evenCount++;
evenSum += userInput;
break;
default:
oddCount++;
oddSum += userInput;
break;
};
}
DisplayResults("Even", evenCount, evenSum);
DisplayResults("Odd", oddCount, oddSum);
return 0;
}
void DisplayResults(char numberType[], int count, int sum)
{
if (total > 0)
{
printf("%s total: %d, %s average: %d\n", numberType, count, numberType, sum/count);
}
else
{
printf("There are no %s numbers!\n", numberType);
}
}Please excuse me if I have the syntax wrong, I haven't used C in about 20 years.
Code Snippets
#include <stdio.h>
void DisplayResults(char numberType[], int total, int sum);
int main(void)
{
int oddCount= 0;
int evenCount= 0;
int evenSum = 0;
int oddSum = 0;
int userInput;
printf("Please enter a number followed by <enter>. Enter \"0\" to exit: \n");
while (((scanf("%d", &userInput)) == 1) && (userInput != 0))
{
switch (userInput%2)
{
case 0:
evenCount++;
evenSum += userInput;
break;
default:
oddCount++;
oddSum += userInput;
break;
};
}
DisplayResults("Even", evenCount, evenSum);
DisplayResults("Odd", oddCount, oddSum);
return 0;
}
void DisplayResults(char numberType[], int count, int sum)
{
if (total > 0)
{
printf("%s total: %d, %s average: %d\n", numberType, count, numberType, sum/count);
}
else
{
printf("There are no %s numbers!\n", numberType);
}
}Context
StackExchange Code Review Q#21007, answer score: 4
Revisions (0)
No revisions yet.