Question

In: Computer Science

In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.

In C

Create a multi-dimensional array and print it out forwards, backwards and then transpose.

Solutions

Expert Solution

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

#include<stdio.h>

int main(){

                //creating constants for storing array sizes (rows and columns)

                const int rows=5, cols=4;

                //creating a 2d array of rows x cols size

                int array[rows][cols];

                //a variable used to assign values to each cell

                int counter=1;

                //looping through each row

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

                                //looping through each column

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

                                               //assigning current value of counter to current position

                                               array[i][j]=counter;

                                               //updating counter

                                               counter++;

                                }

                }

               

                //printing array forwards

                printf("Array in forwards\n");

                //looping through each row

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

                                //looping through each column

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

                                               //printing element at current row and column

                                               printf("%2d ",array[i][j]);

                                }

                                printf("\n");

                }

               

                //printing array backwards

                printf("\nArray in backwards\n");

                //looping through each row from last to first

                for(int i=rows-1;i>=0;i--){

                                //looping through each column from last to first

                                for(int j=cols-1;j>=0;j--){

                                               //printing element

                                               printf("%2d ",array[i][j]);

                                }

                                printf("\n");

                }

               

                //printing transpose

                printf("\nTranspose\n");

                //looping through each column

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

                                //looping through each row

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

                                               printf("%2d ",array[j][i]);

                                }

                                printf("\n");

                }

                return 0;

}

/*OUTPUT*/

Array in forwards

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

17 18 19 20

Array in backwards

20 19 18 17

16 15 14 13

12 11 10 9

8 7 6 5

4 3 2 1

Transpose

1 5 9 13 17

2 6 10 14 18

3 7 11 15 19

4 8 12 16 20


Related Solutions

Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
1. Create a PHP program containing an multi dimensional array of all the first and last...
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order. 2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Please, write a loop to print odd numbers an array a with pointers backwards. You can...
Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size.
Build a two dimensional array out of the following three lists. The array will represent a...
Build a two dimensional array out of the following three lists. The array will represent a deck of cards. The values in dCardValues correspond to the card names in dCardNames. Note that when you make an array all data types must be the same. Apply dSuits to dCardValues and dCardNames by assigning a suit to each set of 13 elements. dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14'] dSuits = ["Clubs","Spades","Diamonds","Hearts"] Once assigned your two dimensional array should resemble this : 2...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT