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
Which Windows installation method requires that you manually rename computers after the installation?​
levacccp [35]

Answer:

Command line

Explanation:

  • After installation of the machine one needs to manually rename the computer. This can be done through the start then settings, then system, and select rename the PC in the right-hand side column.
3 0
3 years ago
What form of note taking would be MOST beneficial for a visual learner who needs to see the connections between ideas?
Svetlanka [38]

Answer:

Think link

Explanation:

8 0
3 years ago
What do you mean by Information Technology explain​
Elena-2011 [213]

Answer:

False

Explanation:

Chloroplasts = photosynthesis

mitochondria= cellular respiration

5 0
3 years ago
Read 2 more answers
Your boss (the IT director) wants to move several internally developed software applications to an alternate environment, suppor
satela [25.4K]

Answer:

A. PaaS

Explanation:

There are various cloud services like PaaS (platform as a service), IaaS (infrastructure as a service), SaaS (software as a service) etc.

The PaaS is a platform based service where online and adaptive environment is available to run software applications on several digital platforms. Unlike SaaS, it does not provide software services but an environment to install and run other applications.

7 0
3 years ago
Security is a major concern with m-commerce. How can m-commerce software ensure the security of transmissions and that the trans
dimaraw [331]

Answer:

Security in communication between applications and sites in m-commerce can use SSL technology.

Explanation:

While developing an app, developers have to perform several checks and ensure that the server has a legitimate certificate.

7 0
2 years ago
Other questions:
  • Select the correct answer.
    6·1 answer
  • Which is the quickest way to change the font color in multiple, randomly located cells in a worksheet
    8·2 answers
  • What does “int” means in php code
    13·1 answer
  • Karen has opened a new business and is using Google Display Ads to build awareness of her new products. How does Google Display
    6·1 answer
  • Which group of commands all appear on the Standard toolbar?
    9·2 answers
  • What are wizard ranks in brainly​
    13·2 answers
  • Ashley works for a company that helps hospitals hire new doctors. They have written a list of strategies that can be used to int
    12·1 answer
  • What sort of negative outcomes are possible for this type of risk?
    14·2 answers
  • What Pre-Built PC should I get? I don't have a lot of money so I'm looking for cheap options.
    8·1 answer
  • 9- Write a program in MARIE to add three numbers.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!