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...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
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...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT