Filling two dimensional array C - c

#include <stdio.h>
int main()
{
//Variables
int x=0,y=0;
int my_data[7][7];
for (x=0;x<9;x++) {
printf("Enter rows:");
scanf("%i",&x);
// printf("%i\n",my_data[x][y]);
}
for(y=0;y<9;y++) {
printf("Enter columns: ");
scanf("%i", &y);
//printf("%i\n",my_data[x][y]);
}
for(y=0;y<9;y++) {
printf("%i\n",my_data[0][y]);
}
return 0; //Return process complete.
}
declares an 8X8 integer array my_data
prompts the user for data to fill the 8X8 array my_data
prints the sum of all the elements for each row
prints the sum of all the elements in the 8X8 array
prints the sum of all the elements in the diagonal of the 8x8 array.
What did I do wrong? When I enter 1,2,3,4,5,6,7,8; 1,2,3,4,5,6,7,8
I get back:
3612392
2686632
1966142592
3604480
0
1966142601
1825884643
4
3612396

Several issues right away:
The array is 7x7, you want it to be int my_data[8][8];
Instead of using the number for the size of the array over and over, define a constant #define FOO 8
The loops are not properly bounded, should be for (x=0;x<8;x++). (again, see my note on using a defined constant for the size)
You are not saving the value to the array, you are saving it to the iterator variable.
With two separate for loops you will not be able to fill the entire table, reconsider this structure because you will likely have to use nested loops.

You can use one loop nested in the other:
#define SIZE 8
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%d,%d): ", i+1, j+1);
scanf("%d", &value);
array[i][j] = value;
}
}

for (x=0;x<9;x++) {
printf("Enter rows:");
scanf("%i",&x);
// printf("%i\n",my_data[x][y]);
}
What happens here is that you change the value of x. You do not save anything on the array.
Similarly for y
Also you should iterate from 0 to 6 (inclusive)
Try
int i;
for(i = 0 ; i < 7 ; i++)
{
scanf("%d" , &my_data[0][x]);
}
Or :
int row , col;
for (row=0; row<7; row++)
{
for (col=0; col<7; col++)
{
scanf("%d" , &ticTacToeBoard[row][col]);
}
}
What you see is the values that your array has when it was declared. The reason for that is when you declare a variable in C without initializing it, as in your case, your program just uses enough memory to store your data into the memory but does not set that memory to 0, hence seeing what the contents of the memory are from previous usage.

int my_data[7][7]; is a 7 x 7 array. Use int my_data[8][8]; for 8 x 8.

my_data is an array with space for 49 int, not 64!
In your for loops you are changing the loop control variable itself by using scanf with its address!
I think you want:
#include <stdio.h>
#define ROWS 8
#define COLUMNS 8
int main(void) {
int my_data[ROWS][COLUMNS];
int rows[ROWS];
int cols[COLUMNS];
size_t i, row, col;
/* input rows */
printf("Enter rows: ");
fflush(stdout);
for (i = 0; i < ROWS; i++) scanf("%d", &rows[i]);
/* input columns */
printf("Enter cols: ");
fflush(stdout);
for (i = 0; i < COLUMNS; i++) scanf("%d", &cols[i]);
/* calculate sums */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLUMNS; col++) {
my_data[row][col] = rows[row] + cols[col];
}
}
/* print results */
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLUMNS; col++) {
printf("%d ", my_data[row][col]);
}
puts("");
}
return 0;
}

Related

C - Printing elements of a 2d array inside a struct array

Im trying to make a function that sets the values of a 2D array, and then another function that prints those values.
For some reason, with my current implementation, if I create a 3x3 matrix and I set every value to the number 5, it prints the values, but it prints them counting up from 5, so it prints 567, 678, 789, when instead I want it to print the exact value that I set.
Here are the function definitions - what am I doing wrong?:
Struct Definition:
struct matrix{
char matrixName[50];
int rows;
int columns;
float* data;
};
typedef struct matrix matrix_t;
Matrix Creation:
int create_matrix(matrix_t* matrixP, int matrixLocation){
char tempName[50];
int rows, cols;
printf("Enter a name for your matrix>\n");
scanf("%s", tempName);
printf("Enter the number of rows>\n");
scanf("%d", &rows);
printf("Enter the number of cols>\n");
scanf("%d", &cols);
float * our_matrix = (float *) malloc(rows * cols * sizeof(float));
strcpy(matrixP[matrixLocation].matrixName, tempName);
matrixP[matrixLocation].rows = rows;
matrixP[matrixLocation].columns = cols;
matrixP[matrixLocation].data = our_matrix;
return 0;
}
Set Values:
int setValues(matrix_t* our_matrix, int matrix_index) {
int counter = 0;
int row = 0, col = 0;
for (col = 1; col <= our_matrix[matrix_index].columns; col++) {
for (row = 1; row <= our_matrix[matrix_index].rows; row++) {
counter++;
printf("Enter the value for column number %d of row number %d>\n", col, row);
scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1));
}
/* separate rows by newlines */
}
return 0;
}
Print:
int printMatrix(matrix_t* our_matrix, int index) {
int row = 0, col = 0;
for (col = 1; col <= our_matrix[index].columns; col++) {
for (row = 1; row <= our_matrix[index].rows; row++) {
printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1));
}
printf("\n");
}
return 0;
}
If I call the printMatrix() function without first using setValues, it seems to print the relative position of the cell that it's printing like so:
But when I call setValues() and set all values to the number 5, its prints counting up from the number 5 like this:
Your position calculation is incorrect. There's one problem and one 'aconventional notation' issue.
The problem is the calculation of the array subscripts:
scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1));
printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1));
You need to multiply the (row - 1) value by our_matrix[matrix_index].cols. You will probably do better with some shorter names for the values:
int max_col = our_matrix[matrix_index].columns;
int max_row = our_matrix[matrix_index].rows;
float *data = our_matrix[matrix_index].data;
for (col = 1; col <= max_col; col++)
{
for (row = 1; row <= max_row; row++)
{
printf("Enter the value for column number %d of row number %d>\n", col, row);
if (scanf("%f", data + (col-1) + (row-1) * max_col) != 1)
{
…report error and do not continue…
}
}
}
for (col = 1; col <= max_col; col++)
{
for (row = 1; row <= max_row; row++)
printf(" %.2f", data[(col-1) + (row-1) * max_col]);
putchar('\n');
}
That deals with the substantive bug. Note the error checking on scanf(). That is important in 'real world' programs (even if you get away without it in classes or in online contests). You could sensibly use the notation &data[(col-1) + (row-1) * max_cols] in place of data + (col-1) + (row-1) * max_cols in the call to scanf() too — that would improve the consistency of the code.
The 'aconventional notation' issue is that in C, array indexing starts at 0, not 1, and you could avoid the multitude of -1 terms by following the C conventions. This snippet also declares the loop variables when they're needed, minimizing their scope. This is a feature of C since C99 — it may not be available to you on some retrograde (but popular and widespread) platforms.
for (int col = 0; col < max_col; col++)
{
for (int row = 0; row < max_row; row++)
printf(" %.2f", data[col + row * max_col]);
putchar('\n');
}

How to write the algorithm of this matrix in this ascending order in C.Where is error in my code?

I am writing this code to print the following matrix in this spiral order(spiral by column).But my code is printing totally different thing.
a a+7 a+8 a+15
a+1 a+6 a+9 a+14
a+2 a+5 a+10 a+13
a+3 a+4 a+11 a+12
Here is what i did:
int main() {
int a;
int Sum = 0;
int i = 0, j = 0,n;
printf("Insert the value of n: ");
scanf("%d",&n);
printf("Insert the value of a number: ");
scanf("%d",&a);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",a);
a = a + 7;
printf("\t");
}
printf("%d",a);
a = a + 1 ;
printf("\n");
}
return 0;
}
The way I approached this is to build the matrix of values you actually want, but doing so in column order, where we can relatively easily control the logic of value progression by row. Then, with that matrix in hand, print out the values in row order, as you want the output:
int main()
{
int a = 7;
int n = 4;
int array[4][4];
for (int c=0; c < n; ++c)
{
for (int r=0; r < n; ++r)
{
// values ascending for even columns
if (c % 2 == 0)
{
array[r][c] = a + c*n + r;
}
// values descending for odd columns
else
{
array[r][c] = a + c*n + n-r-1;
}
}
}
for (int i=0; i < n; ++i)
{
for (int j=0; j < n; ++j)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Output:
Demo here:
Rextester
Instead of using this complex mechanism to keep track of all elements you can just calculate the value to add at any time by simple arithmetic.
See this
int row;
int column;
printf("\n");
for (row = 0; row < n; row++) {
for (column = 0; column < n; column++) {
int base;
int flag;
if (column % 2 != 0) {
base = (column+1)/2 * 2*n - 1;
flag = -1;
}else {
base = column/2 * 2*n;
flag = 1;
}
printf( "%d ", a + base + flag * row);
}
printf("\n");
}
I hope you are able to follow this logic. If not feel free to ask.
Demo here:
Ideone
There seem to be two issues with your code as it is. As mentioned in the above comment, you are using the variable a in the loop calculation, so it is constantly being updated. This means your loop becomes invalid after a few iterations. If you define a dummy variable, this would avoid the problem. Secondly the implementation of the spiralling is close to being right, but it's not quite there.
Consider in the case n = 4. When you print along each row, the difference between a new element and the last alternates between values of (2n - 1) = 7 and 1. To take this into account, you could for example check every time you want to print whether the column index (j) is odd or even, and use this to determine which difference to add. Once you have the row machinery fixed, it shouldn't be difficult to extend it to the columns.
Simple solution using a matrix to calculate values before print them
#include <stdio.h>
int main(void)
{
int a;
int i = 0, j = 0, n;
printf("Insert the value of n: ");
scanf("%d", &n);
printf("Insert the value of a number: ");
scanf("%d", &a);
int matrix[n][n];
for (i=0; i< n*n; i++)
{
// even columns ascending
if (((i/n) % 2) == 0)
{
matrix[i%n][i/n] = a++;
}
// odd column descending
else
{
matrix[n-(i%n)-1][i/n] = a++;
}
}
for (i=0; i< n; i++)
{
for (j=0; j< n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Insert the value of n: 4
Insert start value: 1
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13

For loop continues beyond limit (C language)

I'm trying to make a console application which displays the multiplication table multiplied by an integer. The integer is determined by the user.
It is written in C.
My problem is that if the user enters a value smaller than 10 or greater than 20, the app continues the nested 'for' loop beyond its determined limit (num + 10).
Otherwise it works fine.
#define _CRT_SECURE_NO_WARNINGS
#include `<stdio.h>`
void main()
{
int row, col, num; // Initialization of 10 x 10 array.
int arr[10][10];
printf("Enter a number: "); // User input of integer.
scanf("%d", &num);
for (row = num; row < num + 10; row++) /* Nested for loop. Loop starts at determined
number and stops ten steps further.*/
{
for (col = num; col < num + 10; col++)
{
arr[col][row] = row * col;
printf("%d\t", arr[col][row]);
}
printf("\n");
}
}
You are writing outside arr boundaries. Check your loop logic.
Since you want to multiply by numbers from 1 to 10, then you'll be better of with conditions like this:
for (row=0; row < 10; row++) {
for (col=0; col < 10; col++) {
arr[row][col] = (row+1)*(col+1);
printf("%d\t", arr[row][col]);
}
printf("\n");
}
Rearranging the condition inside the for loop will solve the problem.
the for loop condition will always satisfy for the input less than 10 and greater than 20
As has been stated you're overwriting the bounds of your array which will be overwriting part of the stack - this may be the portion that stores the num value.
Use different counters (i and j for indexing into array):
int main()
{
int row, col, num; // Initialization of 10 x 10 array.
int arr[10][10];
int i = 0;
int j;
printf("Enter a number: "); // User input of integer.
scanf("%d", &num);
for (row = num; row < num + 10; row++) /* Nested for loop. Loop starts at determined
number and stops ten steps further.*/
{
j = 0;
for (col = num; col < num + 10; col++)
{
arr[i][j] = row * col;
printf("%d\t", arr[i][j]);
j++;
}
i++;
printf("\n");
}
return 0;
}
Like Pablo said, you're writing out of your arr boundaries. arr[col][row] is out of bounds when either col or row is not in the range of 0 to 9. Maybe the easiest fix would be to replace this:
arr[col][row] = row * col;
printf("%d\t", arr[col][row]);
with just this line:
printf("%d\t", row * col);
You need to keep inside the limits of your array: arr
Suppose the user enters the value 50, your loop will iterate among 50 and 60, but your array has indexes from 0 to 9 (10 as the total). So, you can offset the indexes, subtracting the base: num
Example - the array offset:
int rowindex, colindex;
for (row = num; row < num + 10; row++) {
rowindex = row - num;
for (col = num; col < num + 10; col++) {
colindex = col - num;
arr[colindex][rowindex] = row * col;
printf("%d\t", arr[colindex][rowindex]);
}
printf("\n");
}
The array bounds for int arr[10][10]; require using index values from 0 to 9.
In addition to paying attention to array bounds in your nested for loops, (which you are currently violating) you should do something to verify user input before using it in the for loop:
printf("Enter a number from 0 to 9: "); // User input of integer.
scanf("%d", &num);
while((num < 0) || (num > 9))// legal array indices are 0 through 9
{
printf("%d is out of bounds, Enter another number: ", num);
scanf("%d", &num);
}
...

How to print first and last row in an array using C

I have m x m array and i am trying to find out first and last row of a given matrix (using C) which is
e.g mat[3][3]={1,2,3,
4,5,6,
7,8,9};
But i could not figure out how to get first and last row using c. If some have the algorithm then please share with me.
Since you have m*m matrix, so first row will be mat[0][1] to mat[0][m-1] and last row will be mat[m-1][0] to mat[m-1][m-1].
for(int i = 0; i < m; i++) {
printf("%d ", mat[0][i]); //first row
}
for(int i = 0; i < m; i++) {
printf("%d ", mat[m-1][i]); //Last row
}
First you should understand how the matrix is organised in memory.
(Taken from http://www.fredosaurus.com/notes-cpp/arrayptr/23two-dim-array-memory-layout.html)
2 Ways you can use - As a 2D array or as a pointer accessed location. Note that everytime the row index moves, the value being accessed has to move by the number of columns in the matrix. Hence accessing the value using a pointer is more intuitive, but is really subjective.
Case1: If 2D array, and ROWS and COLUMNS are known, use the below to print any row.
//row is the row# to print, ex, 0 or ROWS-1
for i from 0 to COLUMNS-1
output = mat[row][i]
Case2: If you have input mat as a pointer to the first value in the array organised as a 2D matrix, use as below
//row is the row# to print, ex, 0 or ROWS-1
for i from 0 to COLUMNS-1
output = mat[row*COLUMNS + i]
As long as you are printing the matrix in the scope where it is declared, you can use the sizeof operator to print the first and last rows (or you pass that information to a function):
#include <stdio.h>
int main (void) {
int mat[3][3]={{1,2,3}, {4,5,6}, {7,8,9}};
unsigned int i;
/* printf first row */
printf ("array[0] : ");
for (i = 0; i < sizeof *mat/sizeof **mat; i++)
printf (" %d", mat[0][i]);
putchar ('\n');
/* print last row */
printf ("array[%lu] : ", sizeof mat/sizeof *mat - 1);
for (i = 0; i < sizeof *mat/sizeof **mat; i++)
printf (" %d", mat[sizeof mat/sizeof *mat - 1][i]);
putchar ('\n');
return 0;
}
Output
$ ./bin/array_sizeof
array[0] : 1 2 3
array[2] : 7 8 9
4X4 Same Code
#include <stdio.h>
int main (void) {
int mat[][4]={{1,2,3,4}, {3,4,5,6}, {6,7,8,9}, {9,8,7,6}};
unsigned int i;
/* printf first row */
printf ("array[0] : ");
for (i = 0; i < sizeof *mat/sizeof **mat; i++)
printf (" %d", mat[0][i]);
putchar ('\n');
/* print last row */
printf ("array[%lu] : ", sizeof mat/sizeof *mat - 1);
for (i = 0; i < sizeof *mat/sizeof **mat; i++)
printf (" %d", mat[sizeof mat/sizeof *mat - 1][i]);
putchar ('\n');
return 0;
}
Output
$ ./bin/array_sizeof
array[0] : 1 2 3 4
array[3] : 9 8 7 6
Passing to a Function
To print the first and last from a function, you must pass the same information. At a minimum, you will always have to pass (1) a pointer to the array, (2) the type and/or sizeof type, (3) the number of rows, and (4) the number of columns. You may be able to specify the type, size and number of columns by passing a pointer to type (combining information for 1, 2 & 4), but that must be passed in some fashion. If your array is filled dynamically, then you will have your count of rows x columns from counting your input. If you declare a static array, you have the constant. Either way, that is a required part of the information. E.g.:
#define NCOL 4
...
int main (void) {
int mat[][NCOL]={{1,2,3,4}, {3,4,5,6}, {6,7,8,9}, {9,8,7,6}};
...
print_1st_last (mat, sizeof mat/sizeof *mat);
...
}
void print_1st_last (int (*mat)[NCOL], size_t m)
{
size_t i;
printf ("array[0] : ");
for (i = 0; i < NCOL; i++)
printf (" %d", mat[0][i]);
putchar ('\n');
/* print last row */
printf ("array[%zu] : ", m - 1);
for (i = 0; i < NCOL; i++)
printf (" %d", mat[m - 1][i]);
putchar ('\n');
}
Create a function that does this for you
void print_first_last(int **array, int height, int width) {
unsigned v;
for (v = 0; v < width; v++) {
printf("%d ", array[0][v]);
}
puts("");
for (v = 0; v < width; v++) {
printf("%d ", array[height - 1][v]);
}
}
//main
print_first_last(mat, 3, 3);
for(int i = 0; i < 2; i++) {
printf("%d ", mat[0][i]); //first row
}
for(int i = 0; i < 2; i++) {
printf("%d ", mat[2][i]); //Last row
}
for (int i = 0; i < m; ++i)
{
// mat[0][i] first row
// mat[m - 1][i] last row
}

Why doesn't work the matrix with 5,3 but 3,5?

I've written this code, but i don't know why it fails with the 5 3 input.
When I give 3 5 it works fine. In every case, if the second number is bigger, It works, but why? I've already tried it with malloc. I'm using Windows 7, Code Blocks 10.05
#include<stdio.h>
#include<stdlib.h>
int main()
{
int **matr;
int row, col, i, j;
scanf("%d", &row); //number of rows
scanf("%d", &col); //number of cols
matr = calloc(col, sizeof(int*)); //creating cols
for(i = 0; i < col; i++)
{
matr[i] = calloc(row, sizeof(int)); //in every col i create an array with the size of row
}
for(j = 0; j < row; j++)
{
for(i = 0; i < col; i++)
{
matr[j][i] = 10; //fill the matrix with number 10
}
}
printf("Matrix is ready\n");
for(j = 0; j < row; j++) //printing the matrix
{
for(i = 0; i < col; i++)
{
printf("%d ", matr[j][i]);
}
printf("\n");
}
return 0;
}
Try matr[i][j] instead of matr[j][i].
In other words, carefully check order of indexes and their ranges.
You correctly allocated a matrix of size [col][row]. And then you access it as if it is a matrix of size [row][col]. No wonder it crashes.
First decide what index interface you want to use - [col][row] or [row][col] - and then either change the allocation of change the access so that they match.
In the calloc, you inverted row and column, so your 2d array size is mixed up.
I have tried it, it worked. So the first memory allocation gives the rows. e.g.
matrix[this part will be given with the first allocation] [the second in the for]
Thanks a lot, I succeded in writing a source code that multiplies matrices :)
I'll vote as soon as i can

Resources