Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Requirement: create one project for each problem; add comments to your code. Sub

ID: 3549060 • Letter: R

Question

Requirement: create one project for each problem; add comments to your code.

Submission: after finishing the all the problems, paste the code and snapshot the output in

one file, submit it to the dropbox.

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

Problem 1:

Write a function to reverse the elements in a one-dimension Array filled with random int

value and print it out (Do NOT use the array index [] notation)

Input: A[5] = {1,3,0,2,5}; Output: {5,2,0,1,3}

Problem 2:

Define the function add_a_to_b(parameters) in the following code .

void main()

{ int a = 10;

int b = 8;

/****** call a function here ****

add_a_to_b (parameters);

**************************/

cout <<

Explanation / Answer

// PROBLEM 1
#include<iostream>
using namespace std;
int main()
{
// Input: A[5] = {1,3,0,2,5}; Output: {5,2,0,1,3}
int A[5] = {1,3,0,2,5};
for(int i=0; i<5/2; i++)
{
int temp = *(A+i);
*(A+i) = *(A+4-i);
*(A+4-i) = temp;
}
for(int i=0;i<5; i++)
cout << A[i] << " ";
system("pause");
return 0;
}

// Problem 2:
// Define the function add_a_to_b(parameters) in the following code .
#include<iostream>
using namespace std;
void add_a_to_b(int a,int& b)
{
b = a + b;
}
int main()
{
int a = 10;
int b = 8;
/****** call a function here ****/
add_a_to_b (a,b );
/**************************/
cout <<" b= " << b<< endl; // output: b = 18
system("pause");

return 0;
}


// Problem 3:
// Write a function to print the sum of each row in a 2D array. For example:
// Input: 3 5 Output: 0 8
// 6 10 1 16

#include<iostream>
using namespace std;
int main()
{
int array[][2] = { {3,5},{6,10}};
for(int i=0; i<2; i++)
{
cout << i << " ";
int sum=0;
for(int j=0;j<2; j++)
sum = sum + array[i][j];
cout << sum << endl;
}
system("pause");
return 0;
}



// ===================================================
// Problem 4 (Extra Credit):
// Write a program to rotate an m x n array by 90 degree clockwise.
// For example, given a MxN (3x4) two dimensional array
/*********************************************
1, 2, 3, 4
5, 6, 7, 8
9,10,11,12
You need to rearrange it into:
9 , 5, 1,
10, 6, 2,
11, 7, 3,
12, 8, 4
***********************************************/

#include<iostream>
using namespace std;
int main()
{
int array[3][4] = { {1, 2, 3, 4},{5, 6, 7, 8},{9,10,11,12}};
int new_array[4][3];

for(int i=0; i<4; i++)
{
for(int j=0; j<3; j++)
{
new_array[i][j] = array[2-j][i];
}
}
for(int i=0; i<4; i++)
{
for(int j=0; j<3; j++)
{
cout << new_array[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote