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

How would I create this sample output without the loadDiagonal and print1Darray

ID: 3724618 • Letter: H

Question

How would I create this sample output without the loadDiagonal and print1Darray functions? I have the first few functions created int findZeros(int arraylIIMAXI, int size): Count the number of zero's in your 2-D array, return the count to the main). int findEvenOdd(int IlMAXI, int): This function takes in the 2D array and size as the parameters and counts the number of evenfor odd) numbers in the array and returns the value. Based on the value returned back to main0. find the other value and print both even count and odd count in main void loadDiagonal(int [MAXI, int, int MAXD: This function takes the 2D array, size and the ID array created in main0 to store the diagonal elements. Load the primary diagonal elements from the 2D array into this array void printIDArray(int all, int): Used to print the 1D array. Use for printing the contents of the diagonal array. This is called after calling the loadDiagonal function. Same as last weeks function. void printTranspose(int MAX], int): Takes the 2D Array and the size as the parameter and prints the contents of the array in a transposed manner, i.e prints the rows as columns and columns as rows. int main(void): Like the prelab, first read the size of the 2D array in the main if the size is 20 display an error message and ask for a new input from, the user. Using this size to initialize the 2D array with the random numbers. Print the array content as shown in the sample output below. Use findZeros to display the number of zero's as shown in the sample output below. Use findEvenOdd to display the number of even and odd numbers in the 2D array. Call the loadDiagonal function by sending the 2D array, size and a 1D array initialized in main0. inside the function load the contents of the ID array with the primary diagonal elements from the 2D array. Then call printiDArray function on the diagonal array you created to print its contents. PrintlDArray is the exact same as the print array function from last week's lab. Next, call the printTranspose function on the 2D array to print the rows and columns in a transposed fashion. Sample Output Characters in bold are input from the user Enter the size of the 2-D array:-1 Please enter a value between 1-20 only: 21 Please enter a value between 1-20 only:6 THE FIRST 2-D ARRAY is: 555011 779219 723672 894703 494074 641162

Explanation / Answer

//header files included
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//macros for MAX value
#define MAX 20

//findZeros function will calculate the no of zeros in the 2D array
int findZeros(int array[][MAX],int size)
{
   int i,j;
   int count_zeros=0;
   for(i=0; i<size; i++){
       for(j=0; j<size; j++){
           //if array element value is 0 then increment the value of zeroscount
           if(array[i][j]==0)
               count_zeros++;
       }
   }

   //return ans
   return count_zeros;
}

//findEvenOdd will count the no of odd and even members of the 2D array
int findEvenOdd(int array[][MAX], int size){
   int i,j;
   int count_even=0;
   for(i=0; i<size; i++){
       for(j=0; j<size; j++){
           //if member is even increment the value of count_even
           if(array[i][j]%2==0)
               count_even++;
       }
   }
   //return value of count of the even members in the 2D array  
   return count_even;
}

//load the primary diagonal elements from the 2D array into the oneD array
void loadDiagonal(int array[][MAX] , int size , int oneD[MAX])
{
   int i,j;
   int count_even=0;
   for(i=0; i<size; i++){
       for(j=0; j<size; j++){
           //if index of member is same then it is diagonal element so add this element in the oneD array
           if(i==j)oneD[i]=array[i][i];
       }
   }
}

//this will print the diagonal array
void print1DArray(int array[],int size){
   int i;
   for(i=0; i<size; i++)
       printf("%d ",array[i] );
   printf(" ");
}

//this will print the transpose of the input matrix
void printTranspose(int array[][MAX],int size){
   int i,j;
   for(i=0; i<size; i++){
       for(j=0;j<size; j++){
           printf("%d ",array[j][i] );
       }
   printf(" ");
   }
}

//initialise the 2D array using the rand function
void initialise_2Darray(int array[MAX][MAX],int size){
   int i,j;
   for(i=0; i<size; i++){
       for(j=0;j<size;j++)
           array[i][j]=rand()%10;
   }
}

//print the 2D arry
void print_2Darray(int array[MAX][MAX],int size){
   int i,j;
   for(i=0;i<size;i++){
       for(j=0;j<size;j++){
           printf("%3d ",array[i][j] );
       }
   printf(" ");
   }
}

//check_error will validate the size of the 2D array as it should be between 1-20
int check_error(int size){
   if(size>=1 && size<=20)return 1;
   else return 0;
}

//BONUS code for finding the number which occurs the max number of times in each row
void findMaxNumber(int array[MAX][MAX], int size){
   int i,j;
   for(i=0;i<size;i++){
       int hash[10]={0};
       for(j=0;j<size;j++){
           hash[array[i][j]]++;
       }
   int k,max=hash[0];
   int val=0;
   for(k=1;k<10;k++){
       if(max<hash[k]){max=hash[k];val=k;}
   }
   printf("ROW %d:--------->%d occurs %d times ",i,val,max );
   }
}


int main(){
   srand(time(NULL));
   int array1[MAX][MAX]={};

   int size;

   printf("Enter the size of the 2D array:");
   scanf("%d",&size);

   while(!check_error(size)){
       printf("Please enter a value between 1-20 only: ");
       scanf("%d",&size);
   }

   initialise_2Darray(array1,size);
  
   printf("THE FIRST 2-D ARRAY IS: ");
   print_2Darray(array1,size);

   printf("The number of zeros in 2-D Array: %d ",findZeros(array1,size) );

   int count_even=findEvenOdd(array1,size);
   int count_odd=(size*size)-count_even;

   printf("The number of Even numbers: %d ",count_even);
   printf("The number of Odd number: %d ",count_odd );

   int ans[MAX];
   loadDiagonal(array1,size,ans);
   printf("The primary diagonal elements in the 2-D arrya,loaded into a 1-D array are:");
   print1DArray(ans,size);
   printf("The transpose of the 2-D array is ");
   printTranspose(array1,size);

   printf("***********BONUS********* ");
   findMaxNumber(array1,size);
}

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