Below is the program I tried.
I get the 2D array and print that array just after the all elements are scanned
like below
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
int R, C;
scanf("%d %d", &R, &C);
int i[C][R];
for (int row = 0; row < R; row++) {
for (int column = 0; column < C; column++) {
int val = 0;
scanf("%d", &val);
i[row][column] = val;
}
}
for (int row = 0; row < R; row++) {
for (int column = 0; column < C; column++) {
printf("%d \t", i[row][column]);
}
printf("\n");
}
printf("RotatedMatrix\n");
return EXIT_SUCCESS;
}
I give the input as 3,4 indicating 3 rows and 4 columns so totally 12 elements
I give the input from 1 to 12 for matrix
But the output matrix looks like below
1 2 3 5
5 6 7 9
9 10 11 12
Instead of
1 2 3 4
5 6 7 8
9 10 11 12
The elements on last column is changed like above
I can't figure out what I am missing. It would be helpful if I get any help
It should be
int i[R][C];
instead of
int i[C][R];
Related
the code I've written creates a pyramid with given input. I need the first part Incrementing by 2, but the code written does not really Incrementing by 2. Aprrechiate any help.
Code:
int main() {
int i, j, n, a, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
printf("%d | ", i);
for(a = 2; a <= i; a+=2) {
printf("%d ", a);
}
for (j = 0; j <= i; j++) {
if (j == i){
printf("%d ", j);
}
}
for(n = 1; n < i; n++){
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Needed Output:
0 | 0
1 | 0 1
2 | 0 2 2 3
3 | 0 2 4 3 4 5
4 | 0 2 4 6 4 5 6 7
5 | 0 2 4 6 8 5 6 7 8 9
6 | 0 2 4 6 8 10 6 7 8 9 10 11
Current Output:
0 | 0
1 | 1
2 | 2 2 3
3 | 2 3 4 5
4 | 2 4 4 5 6 7
5 | 2 4 5 6 7 8 9
6 | 2 4 6 6 7 8 9 10 11
I had to add an extra if condition for i = 0 because it was not fitting in the pattern I figured out. I have done it in just 2 loops instead of you using 3 loops.
Here is the brief explanation. Comment on this answer if you don't understand the code still, I will add a detailed explanation.
Case i = 0 is a special case and it is handled separately using an if.
for i > 0, you can notice that there are 2 series in parallel
even numbers always starting from 0 and if you count them then they are equal to value of i in that row.
another series whose first number is the value of i in that row and it also has number of elements equal to value of i in that row.
That's why in both the loops that handle the series they are run from 0 till i-1.
I hope it helps you and please accept as correct answer if it did.
#include <stdio.h>
int main() {
int i, j, n, a, b, c, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
if(i == 0){
printf("0 | 0\n");
continue;
}
printf("%d | ", i);
// For handling the even number series
for(a = 0, b = 0; a < i; a++, b+=2) {
printf("%d ", b);
}
// For handling the incremental series
for (j = 0, c = i; j < i; j++, c++) {
printf("%d ", c);
}
printf("\n");
}
return 0;
}
You try to overcomplicate simple task.
void pyramid(int rows)
{
for(int row = 0; row < rows; row++)
{
for(int even = 0; even < row * 2; even += 2)
{
printf("%d ", even);
}
if(row == 0) printf("%d ", row);
else
{
for(int remain = row; remain < row * 2; remain++)
{
printf("%d ", remain);
}
}
printf("\n");
}
}
https://godbolt.org/z/cdWqsq
My task is to print out a matrix of integer numbers scanned from the keyboard, and then to print out the numbers under the second diagonal using separate functions. My code looks like this:
#include <stdio.h>
#include <stdlib.h>
//array[n][n] = 2D array of n rows and n columns
//n = number of rows and columns
void printmatrix(int n, int array[n][n]){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%d ", array[i][j]);
}
printf("\n");
}
return;
}
void undersdiagonal(int n, int array[n][n]){
for(int i = 1; i < n; i++){
/**/
for(int j = 1; (j < n); j++){
printf("%d ", array[i][j]);
}
}
return;
}
int main(){
int n;
scanf("%d", &n);
int integermatrix[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &integermatrix[i][j]);
}
}
printf("The entered matrix is:\n");
//calls first function to print matrix
printmatrix(n, integermatrix);
printf("Under the secondary diagonal:\n");
//calls second function to print numbers under main diagonal
undersdiagonal(n,integermatrix);
printf("\n");
return 0;
}
I can't figure out how to print out the numbers under the second main diagonal from the function
void undersdiagonal(int n, int array[n][n]).
My input looks like this:
3
1
2
3
4
5
6
7
8
9
And the output looks like this:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
5 6 8 9
But I want the output to be:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
6 8 9
What could I change in the 'for' loop within the 'for' loop in the function
void undersdiagonal(int n, int array[n][n])
?
The key is with the inner loop of undersdiagonal:
As currently written, for(int j = 1; (j < n); j++){ starts with the second column (column 1) every single time. It needs to start on a different column for each row.
For a 3×3 matrix, the second row (row 1) will begin on the last column (column 2). For a 4×4 matrix, the second row (row 1) will begin on the last column (column 3)... So there is a direct relationship between the starting column (j) and the matrix size: n.
Every time the row number goes up one, the starting column goes down one. So there is an inverse relationship between the starting column (j) and the row: - i.
Take those two pieces together and you have int j = n - i;
I am trying to print the following series: 1 2 3 4 5 6 7 8 9 10 ... ... ...The input of my program contains a single integer n, which determines the number of lines to print.
I've tried to code it but got the following output:
12 33 4 54 5 6 7... ... ...
#include<stdio.h>
int main()
{
int n,i,j,t,m;
scanf("%d", &n);
for(i=1;i<=n;i++)
{
for(j=i,t=1;t<=i;j++,t++)
{
printf("%d ",j);
}
printf("\n");
}
}
To print those numbers, you'll want a counter that starts at 1, increases by 1 every print and is never reset by anything. Adjust your loop like this:
int main()
{
int n, i, j, t = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++, t++)
{
printf("%d ", t);
}
printf("\n");
}
}
Note how t is set to 1, and just gets increased by t++ without resetting like you previously did. Also, you should be printing t, not j.
You should maintain separate counters for the numbers and the number of numbers per line.
int nr = 1, target;
int nrsperline = 1, i;
scanf("%d", &target);
while (nr <= target) {
for (i = 0; i < nrsperline; i++) {
printf("%d ", nr++);
}
printf("\n");
nrsperline++;
}
I wrote a function to print the below pattern.
For example, if the n value is 4 the pattern is
1
2 7
3 6 8
4 5 9 10
Or if the value of n is 5, then the pattern is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
My function gives me the first two block but not the next block. I'm stuck here for long time!
My function is
int printPattern(int n) {
int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
for(row = 1; row <= n; row++) {
fwdCtr = row;
for(column = 1; column <= row; column++) {
if(column % 2 != 0) {
printf("%d ", fwdCtr++);
} else {
evenCtr = fwdCtr + ctr;
printf("%d ", evenCtr);
ctr = ctr - 2;
}
}
printf("\n");
}
}
What I get is
1
2 7
3 6 4
4 5 5 4
Please give suggestions of changes!
The following code should do it:
#include <stdio.h>
void f(int n)
{
for (int i = 0; i < n; ++i)
{
for (int j=0; j<=i; ++j)
{
// Calculate the numbers used so far by previous columns
int x = 0;
for(int v=0; v<j;++v)
{
x = x + (n-v);
}
if ((j % 2) == 0)
{
// even columns
printf("%d ", x+i-j+1);
}
else
{
// odd columns
printf("%d ", x+n-i);
}
}
printf("\n");
}
}
int main(void)
{
f(5);
return 0;
}
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
The easy thing to do is just print the right number based on the row and column and the value of n, like this
int main(void)
{
int n = 20;
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++)
printf("%3d ", 1 + col*n - (col-1)*col/2 + (col%2 ? n-1-row : row-col));
printf("\n");
}
}
I'm trying to create a 2d-array in bubble sort, arranged 25 numbers 5 by 5 in ascending order
my inputs
Enter 25 integers:
Input No.[0][0]: 4
Input No.[0][1]: 5
Input No.[0][2]: 8
Input No.[0][3]: 9
Input No.[0][4]: 4
Input No.[1][0]: 2
Input No.[1][1]: 1
Input No.[1][2]: 0
Input No.[1][3]: 2
Input No.[1][4]: 4
Input No.[2][0]: 6
Input No.[2][1]: 7
Input No.[2][2]: 4
Input No.[2][3]: 5
Input No.[2][4]: 5
Input No.[3][0]: 4
Input No.[3][1]: 8
Input No.[3][2]: 9
Input No.[3][3]: 1
Input No.[3][4]: 2
Input No.[4][0]: 4
Input No.[4][1]: 5
Input No.[4][2]: 2
Input No.[4][3]: 1
Input No.[4][4]: 9
my output shows
Ascending:
4 4 5 8 9
0 1 2 2 4
4 5 5 6 7
1 2 4 8 9
1 2 4 5 9
as you can see its not in proper arranged, it only arranged the 5 numbers each lines not the whole numbers
can anybody help arranged my integers like this
Ascending:
0 1 1 1 2
2 2 2 4 4
4 4 4 4 5
5 5 5 6 7
8 8 9 9 9
this is my code so far
int main(){
int rows = 5, cols = 5;
int arr[rows][cols];
int i,j,k,swap;
printf("Enter 25 integers:\n");
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
printf("Input No.[%d][%d]: ", i+0,j+0);
scanf("%d", &arr[i][j]);
}
}
for(k = 0; k < rows; k++){
for(i = 0 ; i < cols; i++){
for(j = i + 1; j < cols; j++){
if(arr[k][i] > arr[k][j]){
swap = arr[k][i];
arr[k][i] = arr[k][j];
arr[k][j] = swap;
}
}
}
}
printf("Ascending:\n");
for( i = 0 ; i < rows; i++){
for( j = 0 ; j < cols; j++){
printf("%3d", arr[i][j]);
}
printf("\n");
}
getch();
}
Improving on Ahmad's answer, I would like to add the following code (for shorting the table in ascending order):
#include <stdio.h>
#define COL 5
#define ROW 6
int main()
{
int temp, t, i, j;
int arr[ROW][COL]={30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
for(t=1; t<(ROW*COL); t++)
{
for(i=0; i<ROW; i++)
{
for(j=0; j<COL-1; j++)
{
if (arr[i][j]>arr[i][j+1])
{
temp=arr[i][j];
arr[i][j]=arr[i][j+1];
arr[i][j+1]=temp;
}
}
}
for(i=0; i<ROW-1; i++)
{
if (arr[i][COL-1]>arr[i+1][0])
{
temp=arr[i][COL-1];
arr[i][COL-1]=arr[i+1][0];
arr[i+1][0]=temp;
}
}
}
for(i=0; i<ROW; i++)
{
printf("\n");
for(j=0; j<COL; j++)
printf("%3d", arr[i][j]);
}
return 0;
}
replace the input with your table and the definitions with the size of your given array and you're done.
output of the above when executed:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
void twoDimBubbleSort(int** arr, int row, int col) {
for (int i = 0; i < (row * col); ++i) {
for (int j = 0; j < (row * col) - 1; ++j) {
int cr = j / col; // current row
int cc = j % col; // current column
int nr = (j + 1) / col; // next item row
int nc = (j + 1) % col; // next item column
if (arr[cr][cc] > arr[nr][nc])
swap(&arr[cr][cc], &arr[nr][nc]); // any way you want to swap variables
}
}
}
You don't necessarily need to create a 1D array, you can consider your 2D array is a 1D array and transform coordinates when you set/get them.
Consider a structure point with x and y, and ARR_LEN is 5.
int from2Dto1D(point p){ return p.x+ p.y*ARR_LEN;}
Point from1Dto2D(int i){ Point p; p.x = i%ARR_LEN; p.y=i/ARR_LEN; return p;}
Now you can use the normal bubble sorting algorithm with a 1D index on 2D squares array, you just need to convert your index into 2 Point and access/switch data using these Point. (2 because you need a Point with index and a Point with index+1
Put all the array elements from 2-D array to 1-D array then
sort that 1-D array and then put 1-D array in the matrix format
Try this code ....works according to the above given logic
#include<stdio.h>
int main(){
int arr[5][5],l=0;
int result[25],k=0,i,j,temp;
arr[0][0]= 4;
arr[0][1]= 5;
arr[0][2]= 8;
arr[0][3]= 9;
arr[0][4]= 4;
arr[1][0]= 2;
arr[1][1]= 1;
arr[1][2]= 0;
arr[1][3]= 2;
arr[1][4]= 4;
arr[2][0]= 6;
arr[2][1]= 7;
arr[2][2]= 4;
arr[2][3]= 5;
arr[2][4]= 5;
arr[3][0]= 4;
arr[3][1]= 8;
arr[3][2]= 9;
arr[3][3]= 1;
arr[3][4]= 2;
arr[4][0]= 4;
arr[4][1]= 5;
arr[4][2]= 2;
arr[4][3]= 1;
arr[4][4]= 9;
//convert 2 D array in 1 D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
result[k++]=arr[i][j];
}
}
// sort 1 D array
for(i=0;i<25;i++){
for(j=0;j<24;j++){
if(result[j] > result[j+1]){
temp=result[j];
result[j]=result[j+1];
result[j+1]=temp;
}
}
}
/*
for(i=0;i<25;i++){
printf("\n%d",result[i]);
}*/
// convert 1 D array to 2 D array
i=0;
l=0;k=0;
while(i<25){
for(j=0;j<5;j++){
arr[k][j]=result[l];
l++;
}
k++;
i=i+5;
}
//Print matrix i.e 2D array
for(i=0;i<5;i++){
printf("\n");
for(j=0;j<5;j++){
printf(" %d",arr[i][j]);
}
}
}
This works !
#define COL 5
#define ROW 2
int main(){
int temp ;
int arr[2][5]= {2,15,26,14,12,18,1,2,3,4 };
int arr2[10] = {0};
int index = 0 ;
for(int t = 0 ; t<50 ; t++ ){
for (int i =0 ; i < ROW ; i++){
for( int j = 0; j < 5-1 ; j++){
if (arr[i][j] > arr[i][j+1]){
temp = arr[i][j];
arr[i][j] = arr[i][j+1];
arr[i][j+1] = temp;
}
}
//checking for
for( int k = 0 ; k < ROW-1 ; k++){
if (arr[k][COL-1] > arr[k+1][0]){
temp = arr[k][COL-1];
arr[k][COL-1] = arr[k+1][0];
arr[k+1][0] = temp ;
}
}
//---------
}
}
return 0 ;
}