Question

In: Computer Science

1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes...

1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows and columns (3) Write a function that finds the indexes of the largest value stored in a 2D array-of double. (4) Print the resulting indexes in your main function.

Solutions

Expert Solution

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

#include <float.h>

#define length 100

//structure for returning row and column

struct result {

    int row, column;

};

typedef struct result Struct;

  

//function for display 2D array of doubles

void Display_array(int rsize,int csize , double arr[length][length]) {

    int i, j;

    for(i=0;i<rsize;i++){

        for(j=0;j<csize;j++){

            printf("%lf ", arr[i][j]);

        }

        printf("\n");

    }

}


//function for comaring double elements

int compares(double x, double y){

    double xx=0.00000000;

    if((x-y)<xx){

        return 0;

    }

    return 1;

}

//function for returning index of largest element

Struct find(int rsize,int csize , double arr[length][length]){

    int i, j;

    Struct ans;

    int l_element_row=-1, l_element_column=-1;

    double largest=-DBL_MAX;

    for(i=0;i<rsize;i++){

        for(j=0;j<csize;j++){

            if(compares(arr[i][j], largest)){

                largest=arr[i][j];

                l_element_row=i, l_element_column=j;

            }

        }

    }

    ans.row=l_element_row;

    ans.column=l_element_column;

    return ans;

}


int main() {

    int row=0, column=0;

    double arr[length][length];

    //struct answer for returning row and column

    Struct answer;

    //input size of 2D array

    scanf("%d %d",&row,&column);

    //input element of 2D array

    int i, j;

    for(i=0;i<row;i++){

        for(j=0;j<column;j++){

            scanf("%lf",&arr[i][j]);

        }

    }

    //Display 2D array with function Display_array();

    Display_array(row, column, arr);

    //calling functin find for largest element row and column

    answer=find(row , column, arr);

    //printing row and column of largest element in 2D array of Double indexing from 0;

    printf("Largest Element \n Row=> %d \n Column=> %d", answer.row, answer.column);

    return 0;

}


Related Solutions

Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Here is my assignment: Write a program that generates a random number between 1 and 50...
Here is my assignment: Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials...
Write a fortran 90 program that sets up a 4x4 2D real array A and associate...
Write a fortran 90 program that sets up a 4x4 2D real array A and associate a single value pointer AP with element (2,1) of that array. Set all elements in A equal to 0. and print the value of AP to the screen. Next set all elements in A equal to 1.23 and print the value AP to the screen.
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT