How to print first and last row in an array using C - 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
}

Related

Trying to find a elegant way of finding adjacent element in a 2d array in a multi-direction

I want to find the neighboring element that surrounds p. the program below doesn't print of the neighbors
I know we can use brute-force approach, like this:
array[i-1][i]
array[i-1][i-1]
array[i][i-1]
array[i+1][i]
and so on...
But, I worry that it would get tedious to check every possible places of that element p. I am trying to figure out an elegant way to do this.
#include <stdio.h>
#include <stdlib.h>
void draw_board();
int row_number, column_number;
char mark_board(char mark, int row_num, int column_num);
char find_neighborhood(char board[6][6], int i, int j);
char board[6][6] = {
{ '0','0','0','0','0','0' },
{ '0','1','2','3','0','0' },
{ '0','4','P','5','0','0' },
{ '0','6','7','8','0','0' },
{ '0','0','0','0','0','0' },
{ '0','0','0','0','0','0' }
};
int main() {
draw_board();
find_neighborhood(board[6][6], 3, 3); // trying to find neighbor of char p
return 0;
}
void draw_board() {
printf(" 1 2 3 4 5 6\n");
printf(" -----------\n");
for (int i = 0; i < 6; i++) { // rows
printf("%i| ", i + 1);// row number to be printed
for (int j = 0; j < 6; j++) { // columns
printf("%c ", board[i][j]);
}
printf("\n");
}
}
char find_neighborhood(char board[6][6], int row_num, int col_num) {
int rows = sizeof(board); // row limit
int columns = sizeof(board[0]) - 1; // column limit
for (int j = row_num - 1; j <= row_num + 1; j++) {
for (int i = col_num - 1; i <= col_num + 1; i++) {
if (i >= 0 && j >= 0 && i < columns && j < rows && !(j == row_num && i == col_num)) {
printf("The neighbor of p is: %c\n", board[j][i]);
}
}
}
}
In addition to the good points made by #chqlie, you have other areas that pose significant "anti-patters" or just plain "code-smells". The most vexing is your mixing of 1-based rows and columns with 0-based array indexing. In C, all indexing is 0-based. When you write functions manipulating arrays, all indexing should be 0-based. Not only does this make your code easier to follow and maintain, it removes the risk of an off-by-one error associated with mixing 1-based and 0-based indexing.
If you must take 1-based coordinates from the user, then map 1-based indexing to 0-based indexing before you call your array manipulation function. If you don't do it at the point of input, then make it clear to anyone maintaining your code that an indexing changes is occurring. A simple commented function will do, e.g.
/* convert 1-based rows/cols to 0-based indexes */
int toindex (int rc)
{
return rc ? rc - 1 : rc;
}
and call:
/* DANGER mixing 1-based & 0-based indexes !!! */
find_neighborhood (board, toindex (3), toindex (3));
Don't use MagicNumbers in your code. (e.g. 6). This limits your code to a single arrays size requiring picking though all loop and array declarations and recompiling your code just to handle a change of array size. Instead:
#define ROWS 6 /* if you need a constant, #define one (or more) */
#define COLS ROWS
void draw_board (const char board[ROWS][COLS]);
void find_neighborhood (const char board[ROWS][COLS], int row, int col);
This impacts how you write you reading in draw_board() as well, e.g.
void draw_board(const char board[ROWS][COLS])
{
fputs (" ", stdout); /* don't use MagicNumbers */
for (int i = 0; i < ROWS; i++) /* create your headings */
printf ("%2d", i + 1); /* from defined constants */
fputs ("\n ", stdout);
for (int i = 0; i < 2 * COLS - 1; i++)
putchar ('-');
putchar ('\n');
for (int i = 0; i < ROWS; i++) {
printf (" %d|", i);
for (int j = 0; j < COLS; j++) {
printf (j ? " %c" : "%c", board[i][j]);
}
putchar ('\n');
}
}
You have another subtle issue with your declaration of board as char board[6][6] and then your use of the const qualifier in your function parameter lists. Pointers to arrays with different qualifiers are incompatible in ISO C. C11 Standard - 6.7.6.1 Pointer declarators(p2). This is the result of array/pointer conversion on a 2D array resulting in a pointer-to-array of actual type char (*)[6]. Try it, enable full warnings with -Wall -Wextra -pedantic (or /W3 on VS)
As for a "more-elegant" ways to write find_neighborhood() your though of nested loops and bounds checks at the edges is as good as any other approach. You started out in that direction, and other than writing a set of if...else if...else if...else conditionals, it probably a good choice. Eliminating your 1-based/0-based problem it could be written as:
/* all array manipulation functions should use 0-based indexes */
void find_neighborhood (const char board[ROWS][COLS], int row, int col)
{
printf ("\nThe neighbors of '%c' are:\n\n", board[row][col]);
for (int i = row ? row - 1 : row; i <= (row < ROWS - 1 ? row + 1 : row); i++) {
for (int j = col ? col - 1 : col; j <= (col < COLS - 1 ? col + 1 : col); j++) {
if (i == row && j == col)
fputs (" ", stdout);
else
printf (" %c", board[i][j]);
}
putchar ('\n');
}
}
Putting it altogether, you would have:
#include <stdio.h>
#define ROWS 6 /* if you need a constant, #define one (or more) */
#define COLS ROWS
void draw_board (const char board[ROWS][COLS]);
void find_neighborhood (const char board[ROWS][COLS], int row, int col);
/* convert 1-based rows/cols to 0-based indexes */
int toindex (int rc)
{
return rc ? rc - 1 : rc;
}
int main() {
const char board[ROWS][COLS] = { /* avoid global variables */
{ '0','0','0','0','0','0' },
{ '0','1','2','3','0','0' }, /* pointers to arrays with */
{ '0','4','P','5','0','0' }, /* different qualifiers are */
{ '0','6','7','8','0','0' }, /* incompatible in ISO C */
{ '0','0','0','0','0','0' },
{ '0','0','0','0','0','0' },
};
draw_board(board);
/* DANGER mixing 1-based & 0-based indexes !!! */
find_neighborhood (board, toindex (3), toindex (3));
}
void draw_board(const char board[ROWS][COLS])
{
fputs (" ", stdout); /* don't use MagicNumbers */
for (int i = 0; i < ROWS; i++) /* create your headings */
printf ("%2d", i + 1); /* from defined constants */
fputs ("\n ", stdout);
for (int i = 0; i < 2 * COLS - 1; i++)
putchar ('-');
putchar ('\n');
for (int i = 0; i < ROWS; i++) {
printf (" %d|", i);
for (int j = 0; j < COLS; j++) {
printf (j ? " %c" : "%c", board[i][j]);
}
putchar ('\n');
}
}
/* all array manipulation functions should use 0-based indexes */
void find_neighborhood (const char board[ROWS][COLS], int row, int col)
{
printf ("\nThe neighbors of '%c' are:\n\n", board[row][col]);
for (int i = row ? row - 1 : row; i <= (row < ROWS - 1 ? row + 1 : row); i++) {
for (int j = col ? col - 1 : col; j <= (col < COLS - 1 ? col + 1 : col); j++) {
if (i == row && j == col)
fputs (" ", stdout);
else
printf (" %c", board[i][j]);
}
putchar ('\n');
}
}
Example Use/Output
$ ./bin/board_neighbors
1 2 3 4 5 6
-----------
0|0 0 0 0 0 0
1|0 1 2 3 0 0
2|0 4 P 5 0 0
3|0 6 7 8 0 0
4|0 0 0 0 0 0
5|0 0 0 0 0 0
The neighbors of 'P' are:
1 2 3
4 5
6 7 8
When you look for a "more elegant" way of doing anything, in the end you will get a fair amount of opinion built in. Let me know if you have further questions.
There are multiple issues in the code:
find_neighborhood(board[6][6], 3, 3); is incorrect: you should write this instead:
find_neighborhood(board, 3, 3);
in find_neighborhood, the definition int rows = sizeof(board); initializes rows to the size of a pointer, not the number of rows in the matrix. You should use explicit constants or pass the dimensions as extra arguments.
find_neighborhood() performs many tests... But that's the core of the question.
row_number, column_number and board should not be global variables. It is confusing that some functions use the global variables and others take them as arguments (with the same name).
Here is a modified version:
void find_neighborhood(char board[6][6], int row, int col) {
for (int j = max(0, row - 1); j <= min(row + 1, 6); j++) {
for (int i = max(0, col - 1); i <= min(col + 1, 6); i++) {
if (j != row || i != col) {
printf("The neighbor of p is: %c\n", board[j][i]);
}
}
}
}
There is an elegant way to address your objective: you can define board as an 8x8 array where the first and last rows and columns are always empty. The active part of the board has index values in the range 1..6.
Here is a modified version with this approach:
#include <stdio.h>
#include <stdlib.h>
void draw_board(const char board[8][8]);
char mark_board(char board[8][8], char mark, int row, int col);
void find_neighborhood(char board[8][8], int row, int col);
int main() {
int row_number, column_number;
char board[8][8] = {
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, '0','0','0','0','0','0', 0 },
{ 0, '0','1','2','3','0','0', 0 },
{ 0, '0','4','P','5','0','0', 0 },
{ 0, '0','6','7','8','0','0', 0 },
{ 0, '0','0','0','0','0','0', 0 },
{ 0, '0','0','0','0','0','0', 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 },
};
draw_board(board);
find_neighborhood(board, 4, 4); // trying to find neighbors of char p
return 0;
}
void draw_board(const char board[8][8]) {
printf(" 1 2 3 4 5 6\n");
printf(" -----------\n");
for (int i = 1; i <= 6; i++) { // rows
printf("%i| ", i); // row number to be printed
for (int j = 1; j <= 6; j++) { // columns
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void find_neighborhood(char board[8][8], int row, int col) {
char save = board[row][col];
board[row][col] = 0;
for (int j = row - 1; j <= row + 1; j++) {
for (int i = col - 1; i <= col + 1; i++) {
if (board[j][i] != 0) {
printf("The neighbor of p is: %c\n", board[j][i]);
}
}
}
board[row][col] = save;
}

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

How to efficiently store a triangular matrix in memory?

I want to store a lower triangular matrix in memory, without storing all the zeros.
The way I have implemented it is by allocating space for i + 1 elements on the ith row.
However, I am new to dynamic memory allocation in C and something seems to be wrong with my first allocation.
int main ()
{
int i, j;
int **mat1;
int dim;
scanf("%d", &dim);
*mat1 = (int**) calloc(dim, sizeof(int*));
for(i = 0; i < dim; i++)
mat1[i] = (int*) calloc(i + 1, sizeof(int));
for(i = 0; i < dim; i++)
{
for(j = 0; j < i + 1; j++)
{
scanf("%d", &mat1[i][j]);
}
}
/* Print the matrix without the zeros*/
for(i = 0; i < dim; i++)
{
for(j = 0; j < (i + 1); j++)
{
printf("%d%c", mat1[i][j], j != (dim-1) ? ' ' : '\n');
}
}
return 0;
}
If you want to conserve space and the overhead of allocating every row of the matrix, you could implement a triangular matrix by using clever indexing of a single array.
A lower triangular matrix (including diagonals) has the following properties:
Dimension Matrix Elements/row Total elements
1 x . . . 1 1
2 x x . . 2 3
3 x x x . 3 6
4 x x x x 4 10
...
The total number of elements for a given dimension is:
size(d) = 1 + 2 + 3 + ... + d = (d+1)(d/2)
If you lay the rows out consecutively in a single array, you can use the formula above to calculate the offset of a given row and column (both zero-based) inside the matrix:
index(r,c) = size(r-1) + c
The formulas above are for the lower triangular matrix. You can access the upper matrix as if it was a lower matrix by simply reversing the indexes:
index((d-1)-r, (d-1)-c)
If you have concerns about changing the orientation of the array, you can devise a different offset calculation for the upper array, such as:
uindex(r,c) = size(d)-size(d-r) + c-r
Sample code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define TRM_SIZE(dim) (((dim)*(dim+1))/2)
#define TRM_OFFSET(r,c) (TRM_SIZE((r)-1)+(c))
#define TRM_INDEX(m,r,c) ((r)<(c) ? 0 : (m)[TRM_OFFSET((r),(c))])
#define TRM_UINDEX(m,r,c,d) ((r)>(c)?0:(m)[TRM_SIZE(d)-TRM_SIZE((d)-(r))+(c)-(r)])
#define UMACRO 0
int main (void)
{
int i, j, k, dimension;
int *ml, *mu, *mr;
printf ("Enter dimension: ");
if (!scanf ("%2d", &dimension)) {
return 1;
}
ml = calloc (TRM_SIZE(dimension), sizeof *ml);
mu = calloc (TRM_SIZE(dimension), sizeof *mu);
mr = calloc (dimension*dimension, sizeof *mr);
if (!ml || !mu || !mr) {
free (ml);
free (mu);
free (mr);
return 2;
}
/* Initialization */
srand (time (0));
for (i = 0; i < TRM_SIZE(dimension); i++) {
ml[i] = 100.0*rand() / RAND_MAX;
mu[i] = 100.0*rand() / RAND_MAX;
}
/* Multiplication */
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
for (k = 0; k < dimension; k++) {
mr[i*dimension + j] +=
#if UMACRO
TRM_INDEX(ml, i, k) *
TRM_UINDEX(mu, k, j, dimension);
#else
TRM_INDEX(ml, i, k) *
TRM_INDEX(mu, dimension-1-k, dimension-1-j);
#endif
}
}
}
/* Output */
puts ("Lower array");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
printf (" %2d", TRM_INDEX(ml, i, j));
}
putchar ('\n');
}
puts ("Upper array");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
#if UMACRO
printf (" %2d", TRM_UINDEX(mu, i, j, dimension));
#else
printf (" %2d", TRM_INDEX(mu, dimension-1-i, dimension-1-j));
#endif
}
putchar ('\n');
}
puts ("Result");
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
printf (" %5d", mr[i*dimension + j]);
}
putchar ('\n');
}
free (mu);
free (ml);
free (mr);
return 0;
}
Note that this is a trivial example. You could extend it to wrap the matrix pointer inside a structure that also stores the type of the matrix (upper or lower triangular, or square) and the dimensions, and write access functions that operate appropriately depending on the type of matrix.
For any non-trivial use of matrices, you should probably use a third-party library that specializes in matrices.
mat1 = calloc(dim,sizeof(int*));
mat1 is a double pointer.You need to allocate memory for your array of pointers and later you need to allocate memory to each of your pointers individually.No need to cast calloc()
You are dereferencing mat1 at line 8 before it has even been set to point anywhere. You are allocating an array of pointers to int, but you are not assigning that to mat1 but to the dereference of mat1, which is uninitialized, we don't know what it points to.
So this line:
// ERROR: You are saying an unknown memory location should have the value of calloc.
*mat1 = (int**)calloc(dim,sizeof(int*));
Should change to:
// OK: Now you are assigning the allocation to the pointer variable.
mat1 = (int**)calloc(dim,sizeof(int*));

Program in C not working

Ok, so I'm writing a program to make a 10 x 10 array filled with random numbers between 0 & 9, and (with each step organized into a function):
(a) sum the first row and print it out
(b)print out average of main diagonal (top to bottom, left to right)
(c)print out how many 0's are in the first column
(d)make more 10 x 10 arrays with random numbers between 0 & 9 and if all the values in the main
diagonal (top to bottom, left to right) are 7 or greater, print out the array, and the
amount of tries it took. If it can't do it in 1,000,000 attempts, print that it could not
be done.
(e)make a 1D dynamically allocated array containing 10 numbers between -10 & +10, multiply by
the first array made and display the resulting vector
Can't figure out what's not making it work, getting the wrong values for all the steps when they're printed out :'( and some errors
void simple_array(int ten_by_ten[10][10])
{
int i, j;
printf("\n");
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
ten_by_ten[i][j] = rand() % 10;
printf("%d ", ten_by_ten[i][j]);
}
printf("\n");
}
}
void sum_first_row(int y[10][10])
{
int i = 0, j, sum_row = 0;
for (j=0; j<10; ++j)
{
sum_row += y[i][j];
}
printf("\nThe sum of the first row is: %d\n", sum_row);
}
void average_main_diagonal(int z[10][10])
{
int i, j = 0, average_diagonal = 0;
for (i=0; i<10; ++i)
{
++j;
average_diagonal += z[i][j];
}
printf("\nThe average of the diagonal is: %lf\n", (average_diagonal / 10.0));
}
void zeros(int a[10][10])
{
int i, j = 0, zeroz = 0;
for (i=0; i<10; ++i)
{
if (a[i][j] == 0)
++zeroz;
}
printf("\nThere are %d zero's in the first column\n", zeroz);
}
void multiple_arrays()
{
int sum_diagonal = 0,array[10][10], i, j, k, l, c;
while ((sum_diagonal < 70) && (c <= 1000000))
{
j = 0;
k = 0;
l = 0;
i = 0;
for (i=0; i<10; ++i)
{
for (j=0; j<10; ++j)
{
array[i][j] = rand() % 10;
}
}
for (k=0; k<10; ++k)
{
++l;
sum_diagonal += array[k][l];
}
++c;
}
if (c = 1000000)
printf("\nCould not get a diagonal with numbers >= 7\n");
else
{
j = 0;
i = 0;
for (i=0; i<10; ++i);
{
printf("\n");
for (j=0; j<10; ++j)
printf("%d ", array[i][j]);
}
printf("It took %d many tries to get a diagonal with all numbers >= 7", c);
}
}
void array_multiplication(int b)
{
int **arrays, i, j, k, l, m, prod[10];
arrays = (int **) calloc (10, sizeof(int *));
for (i=0; i<10; ++i)
arrays[i] = (int *) calloc (1, sizeof(int));
for (i=0; i<10; i=i+1)
{
arrays[i] = (rand() % 21) -10;
}
for (k=0; k<10; ++k)
{
prod[k] = 0;
for (l=0; l<10; ++l)
prod[k] = prod[k] + b[k][l] * arrays[l];
}
printf ("The product is: <");
for (m=0; m<10; ++m)
printf ("%d, ", prod[m]);
printf (">\n");
}
int main()
{
int x[10][10];
simple_array(x);
sum_first_row(x)
average_main_diagonal(x);
zeros(x);
multiple_arrays();
array_multiplication(x);
return (0);
}
Getting the following errors:
When I comment out the "array multiplication" function (because it's getting the following errors: (a) assignment makes pointer from integer without cast "arrays[i] = (rand() % 21) -10;" (b) value is neither array nor pointer "prod[k] = prod[k] + b[k][l] * arrays[l];" (c) passing arg1 of "array_multiplication" makes integer from pointer without cast "array_multiplication(x);"
and it prints out an incorrect average of the diagonal
Help is Extremely appreciated!!!
Thanks,
--Rob
arrays[i] = (rand() % 21) -10; : arrays[i] is a pointer. You can't assign an integer to a pointer. Actually you just assigned some allocated memory to arrays[i] on the previous line, so even if this worked you would leak that memory.
Maybe you meant to have two nested loops, if you want to put a value in every row and every column?
Also you never free the memory you calloc'd. And don't cast the value returned by calloc either.
The other errors, you meant to declare your function as
void array_multiplication(int b[10][10])
instead of (int b). It is complaining that you are doing array operations on an int.
Your code that looks at the diagonals is incorrect (which you knew). You are incrementing "j" before using it...and so "i" is 0 and "j" is 1...which is not the diagonal. You either need to do your "++j" AFTER using it to look up the value....but would be better suited to just use z[i][i] (use "i" for BOTH indices).
This problem happens in both average_main_diagonal and multiple_arrays.

Filling two dimensional array 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;
}

Resources