This program takes in 10 integer values and stores them in a 2d array. Say if I enter numbers 1 through 10 I expect an output of 1 2 3 4 5 6 7 8 9 10. I don't want it in a matrix format. How can I accomplish this?
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i ) {
for(j=0;j<3;j ) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i ) {
for(j=0;j<3;j ) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
CodePudding user response:
If you don't want it in a matrix format, simply don't print the line breaks. This means you should omit these lines:
if(j==2){
printf("\n");
}
Without line breaks, your output will all be in one line.
Revise your code for displaying the array elements to something like this:
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i ) {
for(j=0;j<3;j ) {
printf("%d ", disp[i][j]);
}
}
CodePudding user response:
If you want it in the sorted way you could use a sorting algorithm. Otherway, If you want it in a single line you could remove the printf("\n")
line.