Question:
C programming problem!! Please help!!?
oo
2013-07-08 13:35:11 UTC
Write a C program that reads integers from user until 0 is entered. After input terminates, the program should report
1. total number of even integers entered.
2. total number of odd integers entered.
3. the minimum value of all integers entered.

here is the way I did it:


int x, num, no_even = 0, no_odd = 0, even, odd;

for(x = 1;; x++)
{
printf("Enter an integer (0 to end): ");
scanf("%d", &num);

if(num == 0)
{
if(num%2 == 0)
{
for(num = 1;;num++)
num = 1;
no_even += num;
}
else
{
for(num = 1;;num++)
num = 1;
no_odd += num;
}
printf("\nTotal number of even integers: %d", no_even);
printf("\nTotal number of odd integers: %d", no_odd);
getch();
}


}


-------------------------------------------------------------------------------------------------------------------------------------------------
Seriously I have no idea how to proceed with this question. Please kindly show me the steps, thanks. ^^
Seven answers:
cja
2013-07-09 07:56:52 UTC
You should write down your design in pseudocode, or some language you understand, so you have clear in your mind what you're trying to do. Then you can start to work on implementing it in C. You should also walk through your code, explaining to yourself what you're doing. You'll find that you have two infinite loops nested inside another infinite loop, with no way out of any of them. To say this is not good is quite an understatement.



Study and understand my working solution below, and you'll certainly learn something.



#include

#include

#include



#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#define MAX_LINE_LEN 32

#define SENTINEL 0

int getInt(void);



int main(int argc, char *argv[]) {

    int n, evenCount = 0, oddCount = 0, minVal = INT_MAX;



    printf("Enter numbers, %d to quit\n", SENTINEL);

    while ((n = getInt()) != SENTINEL) {

        oddCount += n & 1;

        evenCount += !(n & 1);

        minVal = MIN(n, minVal);

    }

    if ((evenCount > 0) || (oddCount > 0)) {

        printf("num even = %d\n", evenCount);

        printf("num odd = %d\n", oddCount);

        printf("minimum = %d\n", minVal);

    }

    return 0;

}



int getInt() {

    bool inputOk;

    char line[MAX_LINE_LEN];

    int z;



    printf("> ");

    for (inputOk = false; inputOk == false; ) {

        fgets(line,MAX_LINE_LEN,stdin);

        inputOk = (sscanf(line,"%d",&z) == 1);

        if (inputOk == false) {

            printf("invalid input, try again: ");

        }

    }

    return z;

}
?
2013-07-08 14:39:19 UTC
This can be done VERY easily using the ternary operator(?:) we go through a single loop and count our numbers:



int main()

{

int num = 1,oddTotal = 0,evenTotal; = 0

while(num != 0)

{

printf("Enter an integer (0 to end): ");

scanf("%i", &num);

oddTotal += num%2 == 1 ? 1:0;

evenTotal += num%2 == 0 ? 1:0;

}

printf("\nTotal number of even integers: %d", no_even);

printf("\nTotal number of odd integers: %d", no_odd);

getch();

return 0;

}



The 2 lines you see with num%2 == 0 ? 1:0 basically mean that if the number is even add 1, if not add 0 and vice versa with the odd. it is a very handy operator and can really reduce your lines of code. Here is a wiki link to learn more about it: http://en.wikipedia.org/wiki/%3F:#C

Good luck!
spark
2013-07-08 13:57:02 UTC
Wow you are confusing me too. Please try to not use variables for multiple things such as num being used as the input and as a counter and as a enumerator (+=num). In that case you could have just put += 1 just to make the code look nicer :D. Also this else statment does not need a for loop in it, you could just put no_odd += 1. Ok enough programming tips this is what you do (in pseudo code)



While (UserInput != 0) { // while input does not equal zero

if (UserInput < Lowest && Lowest != 0) Lowest = UserInput; // This is for the get the lowest value

Else Lowest = UserInput;



if (UserInput%2 == 0) no_even +=1; // A simpler version of what you did

else no_odd += 1;

}
?
2013-07-08 14:35:10 UTC
I fixed some things

untested code follows:





#include

int main(void){

int num,min, no_even = 0, no_odd = 0;

printf("Enter an integer (0 to end): ");

scanf("%d", &num);

min=num;

while(num!=0) {

printf("Enter an integer (0 to end): ");

scanf("%d", &num);

if((num%2)==0)

no_even++;

else

no_odd++;

if(num == 0)

break;

if(num
min=num;

}

printf("\nTotal number of even integers: %d", no_even);

printf("\nTotal number of odd integers: %d", no_odd);

printf("\nThe minimun number is %d\n",min);

getch();

}
anonymous
2013-07-10 02:56:27 UTC
include

#include

#include



enum stringId {

STR_PROMPT,

STR_RESULT_EVEN,

STR_RESULT_ODD,

STR_MIN_VALUE,

STR_RESULT_NONE

};



struct stringy {

enum stringId id;

const char *str;

} english[] = {

{ STR_PROMPT, "Enter an integer (0 to end):" },

{ STR_RESULT_EVEN, "Total number of even integers:" },

{ STR_RESULT_ODD, "Total number of odd integers:" },

{ STR_MIN_VALUE, "Minumum value:" },

{ STR_RESULT_NONE, "None" }

};



const struct stringy *lang = english;



const char *getLocalisedString(enum stringId id, const struct stringy *language)

{

return language[id].str;

}



void init(int *min, unsigned *odd, unsigned *even)

{

*odd = *even = 0;

*min = INT_MAX;

}



int min(int a, int b)

{

int r;



if (a <= b)

r = a;

else

r = max(a, b);



return r;

}



int max(int a, int b)

{

int r;



if (a > b)

r = b;

else

r = min(a, b);



return r;

}



bool isOdd(int n);



bool isEven(int n)

{

bool r;



if (n == 0)

r = true;

else {

int d;

if (n < 0) {

d = 1;

} else {

d = -1;

}

r = isOdd(n + d);

}

return r;

}



bool isOdd(int n)

{

bool r;



if (n == 0)

r = false;

else {

int d;

if (n < 0) {

d = 1;

} else {

d = -1;

}

r = isEven(n + d);

}

return r;



}



void displayPrompt(void)

{

printf("%s ", getLocalisedString(STR_PROMPT, lang));

}



bool getUserInput(int *dest)

{

displayPrompt();

return scanf("%d", dest) == 1 && *dest != 0;

}



void countTheNumbers(int *min, unsigned *odd, unsigned *even)

{

int n;



while ( getUserInput(&n) ) {

*even += isEven(n);

*odd += isOdd(n);

if (n < *min)

*min = n;

}

}



void printResult(int *min, unsigned *odd, unsigned *even)

{

printf("%s %u\n", getLocalisedString(STR_RESULT_EVEN, lang), *even);

printf("%s %u\n", getLocalisedString(STR_RESULT_ODD, lang), *odd);

printf("%s ", getLocalisedString(STR_MIN_VALUE, lang));

printf(!*even && !*odd ? getLocalisedString(STR_RESULT_NONE, lang) : "%d", *min);

printf("\n");

}



int main(void)

{

int min;

unsigned odd, even;



init(&min, &odd, &even);

countTheNumbers(&min, &odd, &even);

printResult(&min, &odd, &even);



return 0;

}
Sadsongs
2013-07-08 16:09:43 UTC
Both roger and Jacob are almost right,. The one thing they don't handle is the input of zero - that should be tested for before you increment either count:



if(number == zero)

{

/* do nothing */

}

else

{

/* process counts */

}
jaisigh
2016-10-13 07:40:06 UTC
write a module to opposite a given string "i'm at a loss for words" ought to alter into "desufnoc ma i" Then chop up the string on areas and bypass each and every word back to the comparable module. "desufnoc ma i" ought to alter into "at a loss for words am i" Thats one way that should artwork


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...