" C " ..stuck at homework, anyone can help ?!

+1 Uldis Dzalbe · November 14, 2015
Conditions! must create programm that contains an input at least 10 int, if entered ' exit ' must kill program, if entered float - must show error at input and ask again input,  and after an input must calculate how much form entered numbers are evens in % , aand that must be stored in array...  im total newbie in programming so... as far as i got it - 


int array[10], i;

for (i = 0 ; i < 10 ; i ++ ); 
printf(" Enter int's :  /n);
scanf(" %d ", &array );
if ("%d"== 'EXIT'){                              // programm must be killed if entered 'exit'
RETURN 0 ;
}
else if ("%d" != "%f"){                                           //cheking if input is valid (int    not float or something)
printf(ERROR!! enter an int :  /n); return;        // error must be shown , and asking for valid input, 
}
else (.......                                                              // and finally calculating evens / odds  in %%

thank you !!

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jon Z. · November 15, 2015
No offence but from your code I can see that you have no idea how this programming language works. Your syntax is terrible and you are trying to do things that you can't do.
I suggest you start watching the video tutorials from the beginning.

Anyway I wrote a program that does what you want, but don't get used to other people doing your homework.

#include <stdio.h>
#include <strings.h>

main()
{
    int array[10], i, j, tmp;
    char input[10];

    i = 0;
    while (i < 10)
    {
        printf("Enter an int:\n");
        gets(input);
        if (strcmp(input, "exit") == 0)  // programm must be killed if entered 'exit'
        {
            return 0 ;
        }
        else
        {
            //cheking if input is valid (int not float or something)
            for(j=0; j<10; j++)
            {
                if(input[j] == '\0')
                {
                    break;  //exit loop if end of string
                }
                if(input[j] == '.') //check if float by checking if input string has a .
                {
                    printf("ERROR!!\n"); // error must be shown , and asking for valid input,
                    continue; //go back to w
                }
            }

            //if we got here, the input is not a float
            tmp = atoi(input); //this converts the string into an int
            if(tmp==0 && strcmp(input, "0")!=0)   //check if valid input
            {
                printf("ERROR!!\n"); // error must be shown , and asking for valid input,
                continue; //go back to w
            }

            //if we got here the input if valid
            array = tmp; //assign the value to the array
            i++;
        }
    }

    //print every element in the array
    j = 0;
    printf("\nArray:\n", array);
    for(i=0; i<10; i++)
    {
        printf("%d - ", array);
        if(array %2 == 0)
        {
            printf("even\n");
            j++;
        }
        else
        {
            printf("odd\n");
        }
    }

    //print how many even and odd
    printf("Even: %d\nOdd: %d\n", j, i-j);

    return 0;
}

  • 1

C

107,295 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator