Write a program that do the following: 1) Declare an array alpha of 10 rows and
ID: 3548963 • Letter: W
Question
Write a program that do the following:
1) Declare an array alpha of 10 rows and 20 columns of type int
2) Initialize the array to 0
3) Store 5 in the first column and make sure the value in each subsequent column is
twice the value in the previous column.
4) Print the array one row per line
Problem 2:
Generate a 3 x 3 array filled with random generated int value within range [a, b],
print all the elements column by column. (a and b are user-defined).
Problem 3:
Generate an m x n array filled with random generated float value; print 1) the
average of all the values on the first row and last column 2) the position(s) of the
smallest value in the array. (m and n are user-defined).
Problem 4:
Write a piece of code to reverse the elements in an one-dimension Array filled with
random int value and print it out (Do NOT use the array index [] notation)
A[5] = {1,3,0,2,5};
Reverse A should be: {5,2,0,1,3}.
Problem 5:
Write a program that prompts the user to input a string and outputs the string in
uppercase letters (using char array).
Explanation / Answer
Write a program that do the following:
1) Declare an array alpha of 10 rows and 20 columns of type int
int alpha[10][20];
2) Initialize the array to 0
for(int i=0; i<10; i++)
for(int j=0; j<20; j++)
alpha[i][j] = 0;
3) Store 5 in the first column and make sure the value in each subsequent column is twice the value in the previous column.
for(int i=0; i<10; i++)
alpha[i][0] = 5;
for(int j=1; i<20; j++)
for(int i=0; i<10; i++)
alpha[i][j] = 2*alpha[i][j-1];
4) Print the array one row per line
for(int i=0; i<10; i++)
{
for(int j=0; j<20; j++)
{
cout << alpha[i][j] << " ";
}
cout << endl;
}
Problem 2:
Generate a 3 x 3 array filled with random generated int value within range [a, b],
print all the elements column by column. (a and b are user-defined).
int a,b;
cin >> a >> b;
int random_array[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
random_array[i][j] = a + (rand()%(b-a));
}
}
for(int j=0; j<3; j++)
{
for(int i=0; i<3; i++)
{
cout << alpha[i][j] << " ";
}
cout << endl;
}
Problem 3:
Generate an m x n array filled with random generated float value; print
1) the average of all the values on the first row and last column
2) the position(s) of the smallest value in the array. (m and n are user-defined).
int m,n;
cin >> m, n;
float** array;
array = new float*[m];
for(int i=0; i<m; i++)
array[i] = new float[n];
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
array[i][j] = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/X));
float sum=0;
for(int j=0; j<n; j++)
sum = sum + array[0][j];
for(int i=0; i<m; i++)
sum = sum + array[i][n-1];
cout <<" average of all the values on the first row and last column is " << (sum/(m+n+1)) << endl;
int min_i = 0;
int min_j = 0;
float min = array[0][0];
for(int i=1; i<m; i++)
for(int j=1; j<n; j++)
{
if(array[i][j] < min )
{
min = array[i][j];
min_i = i;
min_j = j;
}
}
cout << "position(s) of the smallest value in the array row is" << min_i << " col is " << min_j << endl;
Problem 4:
Write a piece of code to reverse the elements in an one-dimension Array filled with
random int value and print it out (Do NOT use the array index [] notation)
A[5] = {1,3,0,2,5};
Reverse A should be: {5,2,0,1,3}.
#include<iostream>
using namespace std;
int main()
{
int A[5] = {1,3,0,2,5};
int len = 5;
for(int i=0; i<len/2; i++)
{
int temp = *(A+i);
*(A+i) = *(A+len-1-i);
*(A+len-1-i) = temp;
}
for(int i=0; i<len; i++)
cout << *(A+i) << " ";
}
Problem 5:
Write a program that prompts the user to input a string and outputs the string in
uppercase letters (using char array).
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char input[20];
cout << "Enter string :";
cin >> input;
cout << endl;
for(int i=0; i<strlen(input); i++)
{
if(input[i]>=97 && input[i]<=121)
input[i] = input[i]-32;
}
cout << input << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.