How to travel reverse diagonally through a 2D array? - c

I have been trying to solve the problem in C.
I can travel through my 2D array diagonally with this code:
for(int k = 0; k<10*2; k++) {
for(int j = 0; j<=k; j++) {
int i = k-j;
if (i <10 && j<10) {
printf("%d ", tomb[i][j]);
}
}
printf("\n");
}
So if my 2D array (tomb) is:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
Then my output for diagonal ( / shape):
0
5 1
10 6 2
15 11 7 3
20 16 12 8 4
21 17 13 9
22 18 14
23 19
24
My questions is, how I could travel through this array in reverse diagonal ( \ shape), so that my output would look like this:
4
3 9
2 8 14
1 7 13 19
0 6 12 18 24
5 11 17 23
10 16 22
15 21
20

Considering the square matrix of dimension = size x size here is the code
for(int i = 0; i < size; ++i) {
int j = size - i - 1;
for(int k = 0; k <= i; ++k,++j) {
printf("%d ",tomb[k][j]);
}
printf("\n");
}
for(int i = 1; i < size; ++i) {
int j = 0;
for(int k = i; k < size ; k++, j++) {
printf("%d ", tomb[k][j]);
}
printf("\n");
}
Whole code demo
https://ide.geeksforgeeks.org/fq59Cm8Hqt Only the code is in Java

Nevermind. The answer is:
for(int k = -5; k<=5; k++) {
for(int j = 0; j<5; j++) {
if ((j-k>=0) &&(j-k<5)) {
printf("%d ", tomb[j][j-k]);
}
}
printf("\n");
}

Related

I've got no idea how to write C-code about pyramid [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I made this by an helper and I tried to unify the location of the sum of each row, but I don't know how to fix it. I think the part of printf("%2d", j) from inner for should be changed, but I have no idea for this.
for (i = 1; i <= s; i++)
{
sumOfRow = 0;
for (j = i; j <= s; j++)
{
printf("%2d", j);
sumOfRow += j;
}
printf("\t\t%-5d\n", sumOfRow);
sum += sumOfRow;
}
printf("Sum of all numbers is %d\n", sum);
getchar();
return 0;
}
#include <stdio.h>
int main()
{
int s, i, j, sum = 0;
// create new variable to store sum of single row
int row_sum = 0;
printf("Enter the size:");
scanf("%d", &s);
for (i = 1; i <= s; i++)
{
row_sum = 0; // reset sum for each row
for (j = i; j <= s; j++)
{
printf("%2d ", j);
row_sum += j; // add current number to row_sum
}
printf("\t%d\n", row_sum);
sum += row_sum; // add current row_sum to total sum
}
printf("%d\n", sum);
getchar();
return 0;
}
EDIT
To add padding, you can use the following trick -
printf("%*s", paddingLength, "");
This will print an empty string that will take "paddingLength" characters.
For each value in a row, we have 3 chars, 2 for the value and one for space. So, add a padding of 3*i before printing row_sum.
#include <stdio.h>
int main()
{
int s, i, j, sum = 0;
int row_sum = 0;
printf("Enter the size:");
scanf("%d", &s);
for (i = 1; i <= s; i++)
{
row_sum = 0;
for (j = i; j <= s; j++)
{
printf("%2d ", j); // this will have fixed length of 3
row_sum += j;
}
printf("\%*s%d\n", 3*i , "", row_sum); // print 3*i whitespace before row_sum
sum += row_sum;
}
printf("%*s%d\n", 3*i, "", sum);
getchar();
return 0;
}
Output -
Enter the size:6
1 2 3 4 5 6 21
2 3 4 5 6 20
3 4 5 6 18
4 5 6 15
5 6 11
6 6
91
Try this
#include <stdio.h>
int main()
{
int s, i, j, ttl=0;
printf("Enter the size:");
scanf("%d", &s);
int sum[s];
for (i = 0; i < s; i++)
{
sum[i]=0;
for (j = i; j < s; j++)
{
printf("%2d", j);
sum[i] += j;
}
printf("\n");
}
for(int k=0; k< s;k++){
printf(" sum of %d is: %d ", k, sum[k]);
ttl += sum[k];
}
printf(" total sum is: %d" , ttl);
getchar();
return 0;
}
A straightforward approach can look the following way.
#include <stdio.h>
int main(void)
{
const unsigned int N = 10;
while ( 1 )
{
printf( "Enter the size: " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < N ) ) n = N - 1;
putchar( '\n' );
const int WIDTH = 25;
unsigned int sum = 0;
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int partial_sum = 0;
for ( unsigned int j = 0; j < n - i; j++ )
{
unsigned int value = i + j + 1;
printf( "%u ", value );
partial_sum += value;
}
printf( "%*u\n", WIDTH - 2 * n + 2 * i, partial_sum );
sum += partial_sum;
}
printf( "Total of the numbers = %u\n\n", sum );
}
return 0;
}
The program output might look like
Enter the size: 6
1 2 3 4 5 6 21
2 3 4 5 6 20
3 4 5 6 18
4 5 6 15
5 6 11
6 6
Total of the numbers = 91
Enter the size: 7
1 2 3 4 5 6 7 28
2 3 4 5 6 7 27
3 4 5 6 7 25
4 5 6 7 22
5 6 7 18
6 7 13
7 7
Total of the numbers = 140
Enter the size: 8
1 2 3 4 5 6 7 8 36
2 3 4 5 6 7 8 35
3 4 5 6 7 8 33
4 5 6 7 8 30
5 6 7 8 26
6 7 8 21
7 8 15
8 8
Total of the numbers = 204
Enter the size: 9
1 2 3 4 5 6 7 8 9 45
2 3 4 5 6 7 8 9 44
3 4 5 6 7 8 9 42
4 5 6 7 8 9 39
5 6 7 8 9 35
6 7 8 9 30
7 8 9 24
8 9 17
9 9
Total of the numbers = 285
Enter the size: 0

fscanf is duplicating numbers while reading from a file

I am trying to read numbers from a file and put them into a 2d array, but the first number of each line is being put into the place of the last number of the previous line.
output should look like this:
11 14 12 07 1 8.7
11 14 11 58 143 8.6
11 14 13 03 163 8.9
11 13 18 06 1 7.3
but instead comes out like this:
11 14 12 07 1 11
11 14 11 58 143 11
11 14 13 03 163 11
11 13 18 06 1 7.3
This is the loop that is putting the values into the array:
double all_data[entry_counter-1][5];
int col_counter = 0;
int row_counter = 0;
for(int i=0; i<=entry_counter/6-1; i++) {
col_counter = 0;
for (int j = 0; j <= 5; j++) {
fscanf(input_file, "%lf\n", &v);
all_data[row_counter][col_counter] = v;
col_counter++;
}
row_counter++;
}
Your code is overly complicated and wrong.
This should do the job:
int cols = 6;
int rows = entry_counter / cols;
double all_data[rows][cols];
for(int row_counter = 0; row_counter < rows; row_counter++) {
for (int col_counter = 0; col_counter < cols; col_counter++) {
fscanf(input_file, "%lf\n", &all_data[row_counter][col_counter]);
}
}
Keep it simple.

How to array the numbers like a bowtie shape in c

I'm trying to arrange numbers in a nxn(n is odd number) matrix with bow tie shapes. (like fig.)
Trying to 5x5 matrix set coordinates but no result.
my code:
bowtie {
int a[5][5] = {{
0,
},
{
0,
},
{
0,
},
{
0,
},
{
0,
}};
int i, j;
int num = 1;
for (i = 0; i < 5; i++) {
if (i <= 2) // y>=0 - coordinate(2d)
{
for (j = i; j <= 2; j++) // x<=0, y>=0 Quadrant 2
{
a[i][j] = num;
num++;
}
for (j = 4 - i; j > i; j++) // Quadrant 1
{
a[i][j] = num;
num++;
}
} else // y<0
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
{
a[i][j] = num;
num++;
}
for (j = i; j >= i; j++) // Quadrant 2
{
a[i][j] = num;
num++;
}
}
for (i = 0; i < 5; j++) {
for (j = 0; j < 5; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
The loop
for (j = 4 - i; j > i; j++) // Quadrant 1
is wrong because j starts at 4 (i valuing 0) being the last valid index then never stop to grow producing an undefined behavior when you go out of the array
The loop
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
is strange because the last possible value is the first one, so this is not a loop but just its body executed with j = 4 - i
The loop
for (j = i; j >= i; j++) // Quadrant 2
is like the first and makes j incompatible with the array dimensions
A proposal where the size in given in argument and can be odd or even :
#include <stdio.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 0; /* the value 1.. to put in the cells */
int empty; /* the empty height */
int i,j;
/* first half and may be center */
empty = -1;
for (j = 0; j <= (n-1)/2; ++j) {
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
if ((n & 1) == 0)
empty += 1;
/* second half */
for (; j < n; ++j) {
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
/* show result */
for (i = 0; i != n; ++i) {
for (j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout); /* witdh = 4 compatible with a size up to 43 */
else
printf("% 4d", a[i][j]); /* width = 4 compatible with a size up to 43 */
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall b.c
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
1 3
2 4
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 4
1 9
2 5 7 10
3 6 8 11
4 12
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 6
1 19
2 7 15 20
3 8 11 13 16 21
4 9 12 14 17 22
5 10 18 23
6 24
pi#raspberrypi:/tmp $ ./a.out 7
1 25
2 8 20 26
3 9 13 17 21 27
4 10 14 16 18 22 28
5 11 15 19 23 29
6 12 24 30
7 31
If you do not accept even size
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i][j]);
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall o.c
pi#raspberrypi:/tmp $ ./a.out
Usage ./a.out <size>
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
invalid size 2
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 17
1 145
2 18 130 146
3 19 33 117 131 147
4 20 34 46 106 118 132 148
5 21 35 47 57 97 107 119 133 149
6 22 36 48 58 66 90 98 108 120 134 150
7 23 37 49 59 67 73 85 91 99 109 121 135 151
8 24 38 50 60 68 74 78 82 86 92 100 110 122 136 152
9 25 39 51 61 69 75 79 81 83 87 93 101 111 123 137 153
10 26 40 52 62 70 76 80 84 88 94 102 112 124 138 154
11 27 41 53 63 71 77 89 95 103 113 125 139 155
12 28 42 54 64 72 96 104 114 126 140 156
13 29 43 55 65 105 115 127 141 157
14 30 44 56 116 128 142 158
15 31 45 129 143 159
16 32 144 160
17 161
pi#raspberrypi:/tmp $
From you remark
'int a[n][n];' has problem.
probably you compiled in C++ rather than C, but it is easy to change that :
replace int a[n][n]; by int * a = malloc(n*n*sizeof(int));
replace each form a[x][y] by a[(x)*n+y]
add a free(a); at the end
For instance if I do that on the proposal only accepting odd size :
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int * a = malloc(n*n*sizeof(int));
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i*n+j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i*n+j]);
}
putchar('\n');
}
free(a);
}
}
return 0;
}

Program in C to generate this pattern

This is the code that I've tried which simply generates numbers and prints. I am totally stuck about how to access the row numbers and interchange the printing positions of the rows of the matrix.
#include <stdio.h>
int main(void)
{
int i,a[10][10],j,n,count=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=count;
printf("%d\t",count++);
}
printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d*\t",a[i][j]);
printf("\n");
}
return 0;
}
I am providing a link for the pattern which is to be printed, please check.
https://drive.google.com/open?id=1DKwW8dQggzNjjtAxwPTEI3nRS9AmpK-2Zw
My approach is to avoid the matrix altogether:
#include <stdio.h>
int main() {
int number;
(void) scanf("%d", &number);
int twice = 2 * number;
int squared = number * number;
for (int row = 0, upward = number, downward = 2 * squared; row < number; row++) {
int n = ((upward > squared) ? downward : upward) - number + 1;
for (int column = 0; column < number; column++) {
printf("%d*\t", n++);
}
printf("\n");
upward += twice;
downward -= twice;
}
return 0;
}
EXAMPLES
> ./a.out
3
1* 2* 3*
7* 8* 9*
4* 5* 6*
> ./a.out
4
1* 2* 3* 4*
9* 10* 11* 12*
13* 14* 15* 16*
5* 6* 7* 8*
> ./a.out
5
1* 2* 3* 4* 5*
11* 12* 13* 14* 15*
21* 22* 23* 24* 25*
16* 17* 18* 19* 20*
6* 7* 8* 9* 10*
>
This is my solution to the problem:
#include <stdio.h>
int main() {
int input = 0;
int i = 0;
int j = 0;
int k = 1;
scanf("%d", &input);
int array[input][input];
for(i = 0; i < (input/2); i++) {
for(j = 0; j < input; j++) {
array[i][j] = k;
k++;
}
for(j = 0; j < input; j++) {
array[input-1-i][j] = k;
k++;
}
}
if((input % 2) == 1) {
for(j = 0; j < input; j++) {
array[input/2][j] = k;
k++;
}
}
for(i = 0; i < input; i++) {
for(j = 0; j < input; j++) {
printf("\t%d", array[i][j]);
}
printf("\n");
}
return 0;
}
These are the outputs of the program for diferent inputs:
1
1
2
1 2
3 4
3
1 2 3
7 8 9
4 5 6
4
1 2 3 4
9 10 11 12
13 14 15 16
5 6 7 8
5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10

zoom two dimentional array C

Solved here is the code that worked for me
for (i = 0; i < nbLine; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColumn) + j));
}
printf("\n");
}
I have a function that output a two dimensional array ([column][line]) and it need to be zoom, in fact it is like going from
1 2
3 4
to a
1 1 2 2
3 3 4 4 array when zoom is equal to 2
here is some code :
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, k, l;
for (i = 0; i < nbColumn; i++)
for (k = 0; k < zoom; k++)
{
for (j = 0; j < nbLine; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}
when I try this code on a squared array, it works fine, but when using a rectangular one, it fails. I have tried to debug it by replacing the printf("%d ", *(array + (i*nbColumn) + j)); by printf("%d ", (i*nbColumn) + j); and it gives me this result for a 8 colomns by 5 rows array :
0 1 2 3 4 5 6 7
5 6 7 8 9 10 11 12
10 11 12 13 14 15 16 17
15 16 17 18 19 20 21 22
20 21 22 23 24 25 26 27
Thanks for help
A working program source is:
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("in.in");
ofstream out("out.out");
int main(void){
int m[100][100],i,j,l,noLines,noColumns,zoom;
in>>noLines>>noColumns>>zoom;
for(i=0;i<noLines;i++){
for(j=0;j<noColumns;j++){
in>>m[i][j];
}
}
for (i = 0; i < noLines; i++){
for (j = 0; j < noColumns; j++){
for (l = 0; l < zoom; l++) {
out<<m[i][j]<<' ';
}
}
out<<'\n';
}
return 0;
}
On the input:
4 3 2
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
you get the output:
1 1 2 2 3 3
4 4 5 5 6 6
7 7 8 8 1 1
2 2 3 3 4 4
Changing the parameters on the first line of the input (i.e. the bidimensional array width, height and the zoom), changes the output.
From your example I can see that you are zooming only horizontally, since the elements are duplicated horizontally, but the number of rows is left intact. So you do not need vertical zoom and therefore your cycle with k is unuseful. This should work:
static void output(int* array, int nbColumn, int nbLine, int zoom)
{
int i, j, l;
for (i = 0; i < nbLine; i++)
{
for (j = 0; j < nbColumn; j++)
for (l = 0; l < zoom; l++)
{
printf("%d ", *(array+ (i*nbColomn) + j));
}
printf("\n");
}
}

Resources