Question

Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num...

Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num is filled completely. Write functions to perform each of the following: a) A function that prints all elements in the array that are greater than 10 or less than 50. b) A function that finds the largest number in the array and returns its subscript. c) A function that finds and returns the average of all the numbers in the array. d) A function that print the entire array in a new table, but starting with the last row and proceeding in reverse order to the first.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/* C program that demonstrates the functions written in the program and display the results of the functions on the console window. */

//main.c
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

//define constant values for number of rows and number of cloumns
#define ROWS 10
#define COLS 7

//funciton prototypes

void fillArray(int num[][COLS]);
void printMoreThan10LessThan50(int num[][COLS]);
void display(int num[][COLS]);
int finLargest(int nums[][COLS], int &rowIndex ,int&colIndex);
void displayReverse(int nums[][COLS]);
double finAverage(int nums[][COLS]);
int main()
{

   //funciton to generate a random values
   srand(time_t(0));

   int largestValue=0;
   //a 2d array of integer type
   int nums[ROWS] [COLS];

   printf("Filling array with some random numbers\n");
   //function calling
   fillArray(nums);
   printf("Dsiplay the array values\n");
   //function calling
   display(nums);
   printf("Printing elements >10 or <50\n");
   //function calling
   printMoreThan10LessThan50(nums);
   int rowIndex =0;
   int colIndex=0;
   //function calling
   largestValue=finLargest(nums, rowIndex ,colIndex);
   printf("\n\nLargest Element : %d\n",largestValue);
   printf("Largest Element row index: %d\n",rowIndex);
   printf("Largest Element column index: %d\n",colIndex);
   //function calling
   printf("\n\nAverage of array values: %.2f\n", finAverage(nums));
   printf("\n\nPrinting 2D array from last row to first row\n");
   //function calling
   displayReverse( nums);


   getch();
   return 0;

}
/*Helper function that fills the array with random values in a range of 1 to 100 */
void fillArray(int nums[][COLS])
{
   int r;
   int c;
   for(r=0;r<ROWS;r++)
   {
       for(c=0;c<COLS;c++)
       {
           nums[r][c]=rand()%100+1;
       }
   }
}
/*d) A function that print the entire array in a new table,
starting with the last row and proceeding in reverse order to the first.*/

void displayReverse(int nums[][COLS])
{
   int r;
   int c;
   for(r=ROWS-1;r>=0;r--)
   {
       for(c=COLS-1;c>=0;c--)
       {
           printf("%-5d",nums[r][c]);
       }
       printf("\n");
   }
}
//Helper function to display the values in the array,nums
void display(int nums[][COLS])
{
   int r;
   int c;
   for(r=0;r<ROWS;r++)
   {
       for(c=0;c<COLS;c++)
       {
           printf("%-5d",nums[r][c]);
       }
       printf("\n");
   }
}
/*a) A function that prints all elements in the array that are greater than 10 or less than 50.*/
void printMoreThan10LessThan50(int nums[][COLS])
{
   int r;
   int c;

   for(r=0;r<ROWS;r++)
   {
       for(c=0;c<COLS;c++)
       {
           if(nums[r][c]>10 && nums[r][c]<50)
               printf("%-5d",nums[r][c]);
       }
   }
}
//b) A function that finds the largest number in the array and returns its subscript.
int finLargest(int nums[][COLS], int &rowIndex ,int&colIndex)
{
   int r;
   int c;
   int largestElement=nums[0][0];
   rowIndex=0;
   colIndex=0;
   for(r=0;r<ROWS;r++)
   {
       for(c=0;c<COLS;c++)
       {
           if(nums[r][c]> largestElement)
           {
               largestElement=nums[r][c];
               rowIndex=r;
               colIndex=c;
           }             
       }
   }

   return largestElement;
}
//c) A function that finds and returns the average of all the numbers in the array.
double finAverage(int nums[][COLS])
{
   int r;
   int c;
   double total=0;
   double average=0;
   for(r=0;r<ROWS;r++)
   {
       for(c=0;c<COLS;c++)
       {
           total=total+nums[r][c];
       }
   }
   average=total/(ROWS*COLS);
   return average;
}

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Assume you have the following declaration (in main) : int num[10] [7]; Assume that array num...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string...

    a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string parameter to initialize the title, a Rectangle object to pass to the Rectangle constructor when creating the canvas_frame object, and a Rectangle object to pass to the Rectangle constructor when creating the canvas object. b. Write a print method that prints the title and prints the perimeter of canvas_frame and the area of canvas - call the methods area and perimeter to get both...

  • Given the prototype: int countPassing(double arr[], int num); Assume that array arr contains num elements with...

    Given the prototype: int countPassing(double arr[], int num); Assume that array arr contains num elements with a value. You may assume that the array has been declared with at least num elements. Write the the function definition including the function header for countPassing() so that the number of values greater than or equal to 69.5 in arr[] is returned.

  • The array declaration in the following C program: main() { int a[10]; } can be replaced...

    The array declaration in the following C program: main() { int a[10]; } can be replaced by which of the following: A #include <stdlib.h> main() { int* a = (int *)malloc(10*sizeof(int)); } B #include <stdlib.h> main() { int[] a = new int[10]; } C #include <stdlib.h> main() { int* a = new int[10]; } D #include <stdlib.h> main() { int* a = (int *)malloc(20*sizeof(float)); }

  • JAVA: Suppose you have the following array declaration and initialization:      int [ ] values;     ...

    JAVA: Suppose you have the following array declaration and initialization:      int [ ] values;      values = new int[18]; Suppose there is a static method called sumAll that is sent an integer array. The sumAll method computes and returns the sum of all the integers in the array that it is sent (assuming the array is filled). Choose the best example of calling this method: Group of answer choices a) sumAll( values[0], values.length ) b) sumAll( values[0 .. 17]...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

  • C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

    C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1)     Prod = M;                       //base case else     Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT