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]
2 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]2 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
An attribute whose value uniquely identifies an object is called a(n) _______.​
Juliette [100K]
This is called a key attribute!

There are different types of key attributes!

Primary key is the key that is actually chosen, for example a social security number. 

There is also a unique key, say our finger prints! they also uniquely identify humans, but usually we prefer to use social security number when we list people (a unique key could be a primary key if we chose so)

We could also identify people by their social security number and a name, but this is unnecessary, as social security number already gives a full identification. Such a reduntant key is called super key. 
6 0
2 years ago
"Write a class named Car that has the following data attributes:" _ _year_model (for the car’s year model) _ _make (for the make
UkoKoshka [18]

Answer:

public class Car {

   private String __year_model;

   private String __make;

   private int __speed;

   //Creating the constructor

   public Car(String __year_model, String __make, int __speed) {

       this.__year_model = __year_model;

       this.__make = __make;

       this.__speed = __speed;

   }

   //Creatining the set and get methods

   public String get__year_model() {

       return __year_model;

   }

   public void set__year_model(String __year_model) {

       this.__year_model = __year_model;

   }

   public String get__make() {

       return __make;

   }

   public void set__make(String __make) {

       this.__make = __make;

   }

   public int get__speed() {

       return __speed;

   }

   public void set__speed(int __speed) {

       this.__speed = __speed;

   }

}

Explanation:

As stated in the question, The class Car is created using Java programming language with the three attributes year_model, make and speed.

Constructors as well as set and get methods were also created for each of the fields.

6 0
2 years ago
The definition of network is:
kondaur [170]

\text{Hello there!}

<u>Networks were originally used as a government weapon 61 years ago</u> to <u>communicate information</u> such as data and research. However, individual networks were eventually discontinued by the government and made open to the public to use for things such as PAN, LAN, MAN, WAN, SAN, and so on.

<u>Our internet today is capable of communicating with bilions of computers.</u> This is possible due to your modem using radio wave-like speeds to connect to your ISP. Your ISP then connects to a larger network, which is connecting to thousands of other networks. You see, <u>the internet is just a large network of networks that are connected through very fast radiowaves</u>. However, it is not just a single network being used anymore; it's thousands of them. The term, "internet" was used to describe this large selection of networks. In short, <u>B</u><u> would be incorrect. </u>

The worldwide web is a protocol used by the internet to connect to select websites favourably from whoever's using it. This obviously would not define the network, as this is something that's used by it. Furthermore, <u>A</u><u> would not be correct.</u>

As described already, the network was a selection of computers used to communicate information to each other. <u>C </u><u>would not be correct </u>as it states that there is only one computer being used.

\fbox{Therefore, D would be the correct answer.}

\rule{300}{1.5}

6 0
2 years ago
Kendall receives an email stating that a leading computer company is giving away free computers, asking her to forward the email
gavmur [86]

Explanation:

Kendall should report the email as scam and delete email instead of forwarding it. She should also run her virus protection software as these kind of emails on the Internet are mostly Fraud and can contain virus so the user should avoid them.

8 0
3 years ago
-1
N76 [4]

Answer:A flowchart is a diagram that depicts the steps involved in solving a problem. The following flowchart shows how to output the multiplication table ( n * 1 to m * 1) of a number, n and m:

3 0
3 years ago
Other questions:
  • Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with a
    13·1 answer
  • Plz tell me a storage device that should be used for this and why
    11·1 answer
  • A virtual private network (VPN) is used to securely connect to another network over a insecure network.
    9·2 answers
  • I need someone who knows HTML to finish the code.
    12·2 answers
  • 8. A pattern of being late for work or for appointments is usually
    12·1 answer
  • how to make assignment on power point plz cntct me and help me all about power point and my assignment is prime ministers of pak
    13·1 answer
  • Explain how each of the five types of prewriting assist a writer in getting started. please make it short.
    15·1 answer
  • What is a relationship between a object and a class?
    7·1 answer
  • One blog may have a greater social influence than another when it has_______?
    12·2 answers
  • What is sun and what does it do?
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!