Question

In: Computer Science

divide even number by 2 and what is the reminder? duvid odd number by 2 and...

divide even number by 2 and what is the reminder?
duvid odd number by 2 and what is the reminder?
here modulus is a remiander function.


MY_ARRAY[] is an array if 49 elements or DINT[49]. Create four versions of structured text code that sets all even elements of MY_ARRAY[] =3 and all odd elements = 7 using. You can use any function or construct in structured text however each version will contain only 1 type of loop construct:
1) 1 While loop, 1 Repeat loop , 1 For loop incrementing by 1 and 2 For loops, each incrementing by 2
Hint, the modulus function may help for even/odd numbers.

Solutions

Expert Solution

  • When you divide an even number by 2, the remainder is 0.
  • When you divide an odd number by 2, the remainder is 1.
  • A number is identified as even number when the number is divided by 2 leaves remainder as 0.
  • A number is identified as odd number when the number is divided by 2 leaves remainder as 1.
  • Modulus is the remainder function in most of the programming languages.
  • Modulus is denoted by '%'

Code Without Snippet Using One For Loop:

#include <iostream>

using namespace std;

int main()
{
// Declare the array of size 49
int my_array[49];
  
// Initialize the array. Initialising with consecutive numbers
for(int i=0;i<49;i++)
{
my_array[i]=i;
}
  
// Printing the array before changing
cout<<"Before Changing: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
  
// Using One For Loop
// Now Replacing every even number with 3 and Odd with 7
for(int i=0;i<49;i++)
{
// Checking if the element is Even
if(my_array[i]%2==0)
{
my_array[i]=3;
}
  
// Checking if the element is Odd
else
{
my_array[i]=7;
}
}
  
cout<<endl;
  
// Printing the result after changing
cout<<"After Changing using For Loop: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}

}

Code With Snippet Using One For Loop:

Output Snippet Using One For Loop:

Code Without Snippet Using While Loop:

#include <iostream>

using namespace std;

int main()
{
// Declare the array of size 49
int my_array[49];
  
// Initialize the array. Initialising with consecutive numbers
for(int i=0;i<49;i++)
{
my_array[i]=i;
}
  
// Printing the array before changing
cout<<"Before Changing: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
  
// Using One While Loop
// Now Replacing every even number with 3 and Odd with 7
int counter=0;
while(counter<49)
{
// Checking if the element is Even
if(my_array[counter]%2==0)
{
my_array[counter]=3;
}
  
// Checking if the element is Odd
else
{
my_array[counter]=7;
}
  
counter++;
}
  
  
cout<<endl;
  
// Printing the result after changing
cout<<"After Changing using While Loop: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
}

Code With Snippet Using While Loop:

Output Snippet Using One While Loop:

Thank you!!!


Related Solutions

Prove that every natural number is odd or even.
Prove that every natural number is odd or even.
Question1. (lesson 2) In the following code we determine a number if its even or odd....
Question1. (lesson 2) In the following code we determine a number if its even or odd. Explain what logic is used for that?    section .text    global _start            ;must be declared for using gcc        _start:                     ;tell linker entry point    mov   ax,   8h           ;getting 8 in the ax    and   ax, 1              ;and ax with 1    jz    evnn    mov   eax, 4             ;system call number (sys_write)    mov   ebx, 1             ;file descriptor (stdout)    mov   ecx, odd_msg       ;message...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Find a system of recurrence relations for the number of n-digit quaternary sequences that contain an even number of 2’s and an odd number of 3’s.
Find a system of recurrence relations for the number of n-digit quaternary sequences that contain an even number of 2’s and an odd number of 3’s. Define the initial conditions for the system. (A quaternary digit is either a 0, 1, 2 or 3)
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
Counts the number of odd, even, and zero digits in an integer input value. Repeat until...
Counts the number of odd, even, and zero digits in an integer input value. Repeat until user does not want to continue. Develop the program in an incremental fashion. For example, write the part of the program, which inputs a single integer value and displays number of odd, even, and zero digits in that number. Submit your partial program for grading to make sure it works for the first few test cases. Below is an example execution of a partial...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
Divide the compartments of the body into 2 and then divide one of them into 2...
Divide the compartments of the body into 2 and then divide one of them into 2 more divisions (hint: ICF & ECF) Where does our body water come from? And how does it leave? In _______________________________________________________________ OUT ____________________________________________________________ Name 3 hormones important in the homeostasis of body sodium levels and describe what they do. __________________________________________________________ ____________________________________________________________ _____________________________________________________________ What is a buffer? _________________________________________________ ________________________________________________________________________ Write the equation for carbonic acid, carbon dioxide and bicarbonate.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT