1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
igomit [66]
3 years ago
5

Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither.

The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers.
Ex: If the input is:

5 2 4 6 8 10
the output is:

all even
Ex: If the input is:

5 1 3 5 7 9
the output is:

all odd
Ex: If the input is:

5 1 2 3 4 5
the output is:

not even or odd
Your program must define and call the following two functions. IsArrayEven returns true if all integers in the array are even and false otherwise. IsArrayOdd returns true if all integers in the array are odd and false otherwise.

bool IsArrayEven(int inputVals[], int numVals)

bool IsArrayOdd(int inputVals[], int numVals)





#include

#include



/* Define your function here */



int main(void) {



/* Type your code here. Remember to include the bool library*/



return 0;

}
Computers and Technology
1 answer:
Andrews [41]3 years ago
7 0

Explanation:

Not sure what level that's at, or how you're expected to manage memory, so I'll write it in a "safe" manner and you can adjust accordingly.

Notably, I'm allocating memory dynamically at the moment.  If you're not used to that, you might want to change that.  Also, the c compiler I'm using (gcc) doesn't handle the bool type, so I used int.  You may need to change that.

Also, if you change it to use fixed arrays instead of malloc, realloc and free, then you don't need to include stdlib.h.

The code below builds successfully with gcc, so if you have any issues, it will be in translating to another compiler/language.

Answer:

#include <stdio.h>

#include <stdlib.h>

int isArrayOdd(int *inputVals, int numVals){

       int n, rval = 1;

       for(n = 0; n < numVals && rval; n++){

               rval &= inputVals[n] & 1;

       }  

       return rval;

}

int isArrayEven(int *inputVals, int numVals){

       int n, rval = 1;

       for(n = 0; n < numVals && rval; n++){

               rval &= 1 - (inputVals[n] & 1);

       }  

       return rval;

}

int main(void) {

       /*  

               I'm writing this to allow any quantity of numbers,

               using malloc to allocate the memory for them dynamically.

               You may want to modify this to match the level that your

               class is in.

       */

       int *numbers, quantity = 0, input;

       numbers = (int *) malloc(sizeof(int));

       printf("Please enter a number, or hit \"q\" to quit: ");

       while(scanf("%i", &input) == 1){

               quantity++;

               numbers = (int *) realloc(numbers, quantity * sizeof(int));

               numbers[quantity - 1] = input;

               printf("\nyou entered %i\n", numbers[quantity - 1]);

       }  

       if(isArrayOdd(numbers, quantity)){

               printf("all odd\n");

       }else if(isArrayEven(numbers, quantity)){

               printf("all even\n");

       }else{

               printf("not even or odd\n");

       }  

       free(numbers);

       return 0;

}

=================================================================

BONUS!    BONUS!    BONUS!    BONUS!    BONUS!    BONUS!    BONUS!

=================================================================

The inefficiency of the logic dictated in the assignment drove me nuts, so here's one that's ridiculously simpler and does exactly the same job:

#include <stdio.h>

int main(void) {

       int input, oddEven = -1;

       printf("Please enter a number, or hit \"q\" to quit: ");

       while(scanf("%i", &input) == 1){

               printf("\nyou entered %i\n", input);

               if(oddEven == -1){

                       oddEven = input & 1;

               }else if(oddEven != (input & 1)){

                       oddEven = 2;

               }

       }

       switch(oddEven){

               case 0:

                       printf("The numbers are all even\n");

                       break;

               case 1:

                       printf("The numbers are all odd\n");

                       break;

               case 2:

                       printf("The numbers are both odd and even\n");

                       break;

               default:

                       // should never happen

                       printf("Something odd happened.");

       }

       return 0;

}

You might be interested in
Formulas should follow the___
tatuchka [14]

Answer:

Order of operations

Explanation:

4 0
2 years ago
Of the following, the greatest advantage of a database architecture is that
Zina [86]

Answer:

c.

Explanation:

Of the following, the greatest advantage of a database architecture is that data redundancy can be reduced. This refers to data being unintentionally repeated within the database causing space to be taken up unnecessarily. Database architecture allows for this problem to be addressed and prevented.

3 0
3 years ago
What is the meaning of HDMI?
tiny-mole [99]

Hey there!

The correct answer is that it stands for high definition video devices.



hope this helped and have a great day (:

3 0
3 years ago
Read 2 more answers
The element of website design that refers to the website's aesthetic appeal and the functional look and feel of the site's layou
zzz [600]

The element of website design that refers to the website's aesthetic appeal and the functional look and feel of the site's layout and visual design is known as context.

<h3>What is webpage context?</h3>

The web page context is known to be a kind of special ID that is is formed with all the new page load event.

The context of a website is known to be the point or place where a person can show their visitors that one care. It gives a kind of website's aesthetic appeal and the functional look.

Learn more about website design from

brainly.com/question/25941596

6 0
1 year ago
Plz answer me will mark as brainliest ​
Vesnalui [34]
I’m pretty sure the answer for 1 is B.
And the answer for 2 is True.
4 0
3 years ago
Other questions:
  • Henrietta, the owner of a very successful hotel chain in the Southeast, is exploring the possibility of expanding the chain into
    15·1 answer
  • Write a couple of paragraph on 'limited government and its important'?​
    7·1 answer
  • What is technology?
    11·1 answer
  • If you were infiltrating a network (10.16.0.0/16), and searching for vulnerabilities (while trying to remain undetected), why wo
    11·2 answers
  • n a​ poll, 6767​% of Internet users are more careful about personal information when using a public​ Wi-Fi hotspot. What is the
    11·1 answer
  • The characteristics of Computer Graphics<br><br>FUEL YOUR<br>GREATNESS​
    9·1 answer
  • Different between internet and email​
    5·1 answer
  • What is the fullform of ETA in computer term​
    15·1 answer
  • For this activity, you will practice being both proactive and reactive to bugs. Both are necessary to get rid of errors in code.
    5·1 answer
  • UPS or FedEx plays what role in the supply?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!