I would love to see the code in c The majority of your devel
I would love to see the code in c++
The majority of your development may take place in the function task2().
1. You need to implement code to output the following line to the screen:
–- Task 2 –-
2. Declare an int array named numbers and initialise it with the following values: 1 4 9 16 9 7 4 9 11
3. Declare a second array of the same size and using a loop populate it with random numbers in the range of 1 – 25 inclusive.
4. Use a loop to compute the ‘total’ of the numbers based on the following rule: If the number being considered is odd then add this to the total, however if the number to be added is even then subtract this from the total. For example for the initialised array it would compute:
1 4 + 9 16 + 9 + 7 4 + 9 + 11 = 22
5. Print both of the arrays and the results of the ‘total’ operation. For example for the initialised array the output could resemble:
[ 1, 4, 9, 16, 9, 7, 4, 9, 11 ] the total is RESULT
Where RESULT is the calculated value from the ‘total’ operation.
Notice that the lists should be comma space separated and that the final element in the list is not terminated with a comma but with a space followed by a square bracket. Also note that each line should begin with a left square bracket followed by a space.
Solution
//Tested on Ubuntu,Linux
#include <iostream>
#include <cstdlib>
#include <ctime>// for Time
using namespace std;
/*Main function start*/
int main(){
/*Variable declaration*/
int RANGE=25; // Holding Range value
int RESULT=0;
int i=0;
srand(time(0));
int number1[]={1,4,9,16,9,7,4,9,11};
/*calcualting length of an array*/
int len=(sizeof(number1)/sizeof(*number1));
int *number2=new int[len];
/*Filling array number2 with random number*/
for(i=0;i<len;i++){
number2[i]=(rand() % RANGE + 1);
}
/*Calcualting Total for first array*/
std::cout<<\"[\";
for(int j=0;j<len;j++){
/*If number is even then substract from result
* else add this number to result*/
if(number1[j]%2==0){
RESULT=RESULT-number1[j];
}else{
RESULT=RESULT+number1[j];
}
if(j+1<len){
std::cout<<number1[j]<<\",\";
}else{
std::cout<<number1[j]<<\"]\";
}
}
std::cout<<\" the total is \"<<RESULT<<std::endl;
RESULT=0;
std::cout<<\"[\";
/*Calculating RESULT for second array of random integer value*/
for(int j=0;j<len;j++){
/*If number is even then substract from result
* else add this number to result*/
if(number1[j]%2==0){
RESULT=RESULT-number2[j];
}else{
RESULT=RESULT+number2[j];
}
if(j+1<len){
std::cout<<number2[j]<<\",\";
}else{
std::cout<<number2[j]<<\"]\";
}
}
std::cout<<\" the total is \"<<RESULT<<std::endl;
return 0;
}
/**************output**************/
anshu@anshu:~/Desktop/chegg$ g++ ArrayOperation.cpp
anshu@anshu:~/Desktop/chegg$ ./a.out
[1,4,9,16,9,7,4,9,11] the total is 22
[5,23,23,7,6,7,5,1,3] the total is 10
Thanks a lot

