Question

*Answer must be in C* Write a program that: 1- Declare and initialize all values to...

*Answer must be in C*

Write a program that:

1- Declare and initialize all values to zero an array of doubles 10 x 10 named "cat"

2- Write the code(using for loops) to fill the array with values 1, 2, 3, .... 98, 99, 100

3- Output (using for loops) the sum of the diagonal from 1-100

4- Output (using for loops) the sum of the last 50 rows of the array

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: The last question (the sum of the last 50 rows of the array) is confusing. There are only 10 rows in the array, so I meant last 5 rows (second half) instead of 50. Also assuming you meant the sum of all elements in all those rows, instead of finding sum row by row separately. So if this is anything different than what I understood, please let me know in comments, I’ll correct it as soon as I can.

#include<stdio.h>

int main(){

                //creating a 10x10 array initialized with 0s

                int cat[10][10]={0};

               

                int value=1;

                //looping through each row

                for(int i=0;i<10;i++){

                                //looping through each column

                                for(int j=0;j<10;j++){

                                               //setting value to element at row i and column j

                                               cat[i][j]=value;

                                               //incrementing value

                                               value++;

                                }

                }

               

                //initializing sum of diagonals to 0

                int sumDiagonal=0;

                //looping from i=0 to i=9

                for(int i=0;i<10;i++){

                                //adding element at row i, column i to sumDiagonal

                                //note: indices of diagonal goes from (0,0), (1,1), (2,2) etc.. upto (9,9)

                                sumDiagonal+=cat[i][i];

                }

                //printing sum of diagonal

                printf("sum of diagonal from 1-100 = %d\n",sumDiagonal);

                               

                //now, in the question it says to compute the sum of last 50 rows. but there are no

                //50 rows, the array has 10 rows and 10 columns only. so i'm assuming that you meant

                //last 5 rows (second half) instead of 50 rows

                int sumLast5rows=0;

               

                //starting from 6th row to 10th row, finding sum of all values

                for(int i=5;i<10;i++){

                                for(int j=0;j<10;j++){

                                               sumLast5rows += cat[i][j];

                                }

                }

                //printing sum

                printf("sum of all elements in last 5 rows = %d\n",sumLast5rows);

                return 0;

}

/*OUTPUT*/

sum of diagonal from 1-100 = 505

sum of all elements in last 5 rows = 3775

Add a comment
Know the answer?
Add Answer to:
*Answer must be in C* Write a program that: 1- Declare and initialize all values to...
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
  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • Write a Java program to create a two-dimensional array, myFifthMatrix, with the following specifications:  Initialize...

    Write a Java program to create a two-dimensional array, myFifthMatrix, with the following specifications:  Initialize myFifthMatrix with int type and with size [3][3].  Use a for loop to fill the myFifthMatrix with random values between 0 and 99.  Print the myFifthMatrix using a for loop.  For each column, use a variable named total to store its sum. Add each element in the column to total using a for loop

  • *C CODE* 1. Declare an array of doubles of size 75 called scoreBonus and initialize all...

    *C CODE* 1. Declare an array of doubles of size 75 called scoreBonus and initialize all the elements in the array to 10.5 2. Declare an array of integers called studentNumbers The array can hold 112 integers Prompt and get a number from the user for the third element in the array Prompt and get a number from the user for the last element in the array

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • Write a program demonstrating your understanding of arrays: Declare, initialize and printout the contents of the...

    Write a program demonstrating your understanding of arrays: Declare, initialize and printout the contents of the following: 1. an integer array with 100 random values in it (select your random numbers from this domain: [1,1000])        To instantiate a random number object put the following line in the Main() method at the beginning of the function:                Random rndm = new Random();   // thanks Nicolas and Alexander!!         To ask for a random number from the random number object put...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • Description: Write a complete C++ program that will do the following: 1. Declare an array of...

    Description: Write a complete C++ program that will do the following: 1. Declare an array of 30 elements 2. Set the array to random numbers between 0 to 100 inclusive. Don't seed the random number generator. 3. Output the array. 4. Sort the array in ascending order using the selection sort. 5. Output the sorted array. Required IO: Array by F. Last Original: 1 5 ... 2 10 + Sorted: 1 2 ... 5 10 ( +

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

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