Question

In: Computer Science

Problem Description: In C write a program that assigns random integers between 1 and 20 to...

Problem Description:

In C write a program that assigns random integers between 1 and 20 to a 5 x 5 two-dimensional array then displays the array with average of each row in a table format and the total of all elements in the array. Also calculate the total of each diagonal, top left to bottom right and top right to bottom left. Display the largest of the diagonal’s totals.

Example of the array:

6          10        3          19        20       

1          17        13        18        1         

18        7          14        9          2

16        11        8          9          10

6          15        5          12        2

Sample output:

                                                Avg.

6          10        3          19        20        ???

1          17        13        18        1          ???

18        7          14        9          2          ???

16        11        8          9          10        ???

6          15        5          12        2          ???

Total of element in array: ????

Total of diagonal, top left to bottom right: ????

Total of diagonal, top right to bottom left: ????

Top right to bottom left diagonal total have the largest value of ???

Requirement:

  1. You must submit a function call diagram for the program. Make sure the diagram is legible and submit a pdf or an image file
  2. Must use two-dimensional array to calculate total of all elements
  3. Must create constants for size of the array
  4. Must copy each row into a single dimensional array, pass the single dimensional array to a function to calculate the average of each row. One function only for this requirement.
  5. Must use a function to generate a random number
  6. Must use a function to find the largest of the two diagonals

Solutions

Expert Solution

In this program, two constant variables have used, r for row and c for column, both of them have the value 5. rand() is used to generate the random value between 1 and 20 using the formula rand()%(upper+1-lower)+1, ie rand()%(20+1-1)+1. d1 and d2 are used to store the sum of diagoals and a one dimensional array b is used to store each row and is passed to avg_row function to find the average of each row. A function diag_comp is used to compare the total of both diagonals. The function ran_val is defined to generate random values and is called inside the loop to generate 25 values( 5row x 5 cols).

Program code and output screen is given for your reference.

/* Main function of the C program. */

#include <stdio.h>
#include <stdlib.h>

#define r 5//constant for rows
#define c 5//constat for cols

int ran_val();//for random values
float avg_row(int b[10]); //to calucate average of each row
void diag_comp(int x,int y);//to compare diagonal totals
int main()
{

int a[r][c];
int b[c];
int i,j,t=0,d1=0,d2=0;
float avg;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i][j]=ran_val();
}
}
  
printf("\t\t avg\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t=t+a[i][j];
printf("%d ",a[i][j]);
b[j]=a[i][j];
}
  
avg=avg_row(b);//calling function to calculate average of each row
printf(" %f",avg);
printf("\n");
}
printf(" \nTotal of elements in array : %d \n ",t);
  
  
//calculating total of diagonal from top left to bottom right
for(i=0;i<r;i++)
{
d1=d1+a[i][i];
}
printf("\nTotal of diagonal, top left to bottom right: %d\n",d1);

// calculating total of daiagonal from top right to bottom left:
for(i=0,j=c-1;i<r;i++,j--)
{
d2=d2+a[i][j];
}
printf("\nTotoal of diagonal,top right to bottom left: %d \n",d2);

diag_comp(d1,d2);//calling the function to compare diagonal totals.
return 0;
}

int ran_val()//function for generating random values
{
int a;
a=rand()%(20+1-1)+1;//generating random values between 1 and 20
return a;
}

float avg_row(int b[10])
{
int sum=0,i,k;
float avg;
  
for (i=0;i<c;i++)
{
sum=sum+b[i];
  
}
  
avg=(float)sum/c;
return avg;
}

void diag_comp(int x, int y)//comparing diagonal totals
{
if(x>y)
{
printf("\nTop left to bottom right diagonal total have the largest value of : %d ", x );
}
else
{
printf("\nTop right to bottom left diagonal total have the largest value of : %d \n", y );
}
}

/* Main function of the C program. */

#include <stdio.h>
#include <stdlib.h>

#define r 5//constant for rows
#define c 5//constat for cols

int ran_val();//for random values
float avg_row(int b[10]); //to calucate average of each row
void diag_comp(int x,int y);//to compare diagonal totals
int main()
{
   
  int a[r][c];
  int b[c];
  int i,j,t=0,d1=0,d2=0;
  float avg;
  for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
          a[i][j]=ran_val();
      }
  }
  
   printf("\t\t avg\n");
   for(i=0;i<r;i++)
  {
      for(j=0;j<c;j++)
      {
          t=t+a[i][j];
          printf("%d ",a[i][j]);
          b[j]=a[i][j];
      }
    
      avg=avg_row(b);//calling function to calculate average of each row
      printf(" %f",avg);
      printf("\n");
  }
  printf(" \nTotal of elements in array : %d \n ",t);
  
  
   //calculating total of diagonal from top left to bottom right
   for(i=0;i<r;i++)
   {
     d1=d1+a[i][i];  
   }
   printf("\nTotal of diagonal, top left to bottom right: %d\n",d1);
   
   // calculating total of daiagonal from top right to bottom left:
   for(i=0,j=c-1;i<r;i++,j--)
   {
       d2=d2+a[i][j];
   }
   printf("\nTotoal of diagonal,top right to bottom left: %d \n",d2);
   
   diag_comp(d1,d2);//calling the function to compare diagonal totals.
   return 0;
}

int ran_val()//function for generating random values
{
    int a;
    a=rand()%(20+1-1)+1;//generating random values between 1 and 20
    return a;
}

float avg_row(int b[10])
{
    int sum=0,i,k;
    float avg;
      
    for (i=0;i<c;i++)
    {
        sum=sum+b[i];
        
    }
    
    avg=(float)sum/c;
    return avg;
}

void diag_comp(int x, int y)//comparing diagonal totals
{
    if(x>y)
    {
        printf("\nTop left to bottom right diagonal total have the largest value of : %d ", x );
    }
    else
    {
        printf("\nTop right to bottom left diagonal total have the largest value of : %d  \n", y );
    }
}

OUTPUT SCREEN

Function call diagram


Related Solutions

Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive. A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101). You must implement and use the following functions as prototyped below: /// --------------------------------------------------------------- ///...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT