int a[3][4], i, j;
int row_total;
printf("Enter matrix values : \n");
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 4; j++)
scanf("%d", &a[i][j]);
}
row_total *= a[i][j];
if (row_total > 0) {
printf("%d\n", row_total);
}
The matrix in the example has 3 rows and 4 columns. The values of this matrix are determined by the user. There are examples everywhere in the form of the product of 2 matrices. But I couldn't find how to process the values in rows of a single matrix. I need to multiply the values that the user enters for the row. How can I find the product of the values written in the rows of the matrix?
In the last part, I just tried to print the values to see if I could read the matrix multiplication of the code. But I could not read the values and get any multiplication result.
initialization
Always initialize your variables
because when you don't initialize it, it will have a "garbage" value.
You had problems reading the values because of the garbage value in row_total.
https://stackoverflow.com/a/1597426/17652416
Arrays
Pay attention that arrays start from 0 not 1 so if the amount of rows is 4 for
example, the rows will be - 0, 1, 2, 3 and not 1, 2, 3, 4.
So the iterations should start from i = 0; i < 4 and not i = 1; i <= 4.
More about arrays in C
Fixed initialization
Defines
#define ROWS 3
#define COLS 4
int a[ROWS][COLS] = { 0 };
int row = 0, col = 0;
int row_total = 1;
printf("Enter matrix values : \n");
//Get the values for the matrix from the user
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
scanf("%d", &a[row][col]);
}
}
Iterating on the rows - the answer
//Print the product of every row
for (row = 0; row < ROWS; row++)
{
row_total = 1;
for (col = 0; col < COLS; col++)
{
row_total *= a[row][col];
}
printf("The product of row %d is: %d\n", row, row_total);
}
Related
Given a 2-Dimensional Array, sum up all the numbers that are not on the edges of the 2-D array.
Example 2-D Array:
5 7 1 6
2 6 1 8
1 5 4 7
5 8 9 1
4 4 5 1
The numbers added should only be 6+1+5+4+8+9.
int rows, cols,s1=0,s2=0;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("Elements:\n");
for(int row = 0; row < rows; row++){
for(int col = 0; col < cols; col++){
scanf("%d", &matrix[row][col]);
}
}
printf(Sum: %d", __);
......
Write a function:
static int sum_middle(int rows, int cols, int matrix[rows][cols])
{
int sum = 0;
for (int r = 1; r < rows - 1; r++)
{
for (int c = 1; c < cols - 1; c++)
sum += matrix[r][c];
}
return sum;
}
Then call it after your input loops:
int sum = sum_middle(rows, cols, matrix);
You could change your input functionality to calculate the sum as you go along:
int sum = 0;
for(int row = 0; row < rows; row++){
for(int col = 0; col < cols; col++){
scanf("%d", &matrix[row][col]);
if(
row && (rows > 2) && (row + 1) != rows &&
col && (cols > 2) && (col + 1) != cols
)
sum += matrix[row][col];
}
}
Or you could do this in a 2nd loop (see #JonathanLeffler's answer). This would allow you to do the rows and cols check only once instead of per iteration (i.e. if rows = INT_MAX and cols is 0, 1 or 2).
Let me explain you how to do this in pseudo-code:
You need to sum all numbers which don't pass a certain criteria. There are two ways to do this:
Sum all number which don't pass a certain criteria. This can be done if the criteria is not too difficult.
Sum all numbers. Sum all numbers who pass a certain criteria and subtract that from the first sum.
Let's see if we can accomplish in the first way: we start by summing all numbers:
sum = 0
for i = 0 to a-1:
for j = 0 to b-1:
sum += matrix[i,j];
next j
next i
Now we need to add the criteria:
sum = 0
for i = 0 to a-1:
for j = 0 to b-1:
if not(criteria(i,j))
then sum += matrix[i,j];
next j
next i
But what is the criteria: how to you say (i,j) belongs to an edge?
Well, that's quite simple: the edges mean that i equals either 0 or a, and j equals 0 or b:
So you write a function criteria(i,j) as follows:
boolean criteria(i,j):
return ((i == 0) OR (i == a)) AND
((j == 0) OR (j == b));
Or, as the criteria is so simple, you don't even need a function for this:
sum = 0
for i = 0 to a-1:
for j = 0 to b-1:
if not(((i == 0) OR (i == a)) AND
((j == 0) OR (j == b)))
then sum += matrix[i,j];
next j
next i
But as you might expect, there's an easier way to write the criteria, using not(x AND y) = not(x) OR not(y) and not (x OR y) = not(x) AND not(y), but this one I leave to you :-)
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');
}
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);
}
...
Sorry for the vague title, I wasn't really sure how to explain. I'm trying to solve the Eight Queens puzzle.
For those unfamiliar with the eight queens puzzle:
This program is supposed to find a possible way that 8 queens can
be placed on an 8x8 chessboard so that the queens cannot
capture one another -- that is, so that no column, row, or
diagonal is occupied by more than one queen.
The way I have it mapped out in my head is:
1) I'll set the whole array: chess[8][8] = {2}
2) Go to the beginning of the array, and as long as chess[i][j] == 2, it will be reassigned to 1. Then as soon as that happens, I have another program block called set_illegal, which will go and set the diagonals, row, and column to 0. (For that reason I will have to have chess[8][8] be a global variable.)
3) After set_illegal is finished, the test program will jump back into the loop of assign_q and the process will start all over again.
4) After this it will print out the solution. Unfortunately I didn't code this find multiple solutions... so it will only display 1 solution (kind of nooby programming lol...)
Any input is much appreciated!!
#include <stdio.h>
#include <stdlib.h>
#define N 8
int chess[N][N] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, row1, column1;
//the reason for row1 and column1 is so that set_row, set_column, and set_diagonal
//will have a spot to start. Also I could have assigned all the elements in the array to 2 using a loop, but I was feeling a little lazy...
void assign_q(int **chess[N][N]**);
int main()
{
int row, column;
assign_q(chess);
for (row = 0; row < N; row++) //prints the whole table
{
for (column = 0; column < N; column++)
printf("%d ", chess[row][column]);
printf("\n");
}
return 0;
}
void set_illegal(void);
void assign_q(int chess[N][N])
{
int row, column;
for (column = 0; column < N; column++)
{
for (row = 0; row < N; row++)
{
if (chess[row][column] == 2) //if the element of the array is equal to 2, then it will set it to 1
{
chess[row][column] = 1;
row1 = column;
column1 = column;
set_illegal(); //goes through the column, row, and diagonal to set them all illegal
break;
}
}
}
}
void set_column(void);
void set_row(void);
void set_diagonal(void);
void set_illegal()
{
set_column();
set_row();
set_diagonal();
}
void set_column()
{
int row;
for (row = 0; row < N; row++)
chess[row][column1] = 0; //sets the column illegal
}
void set_row()
{
int column;
for (column = 0; column < N; column++)
chess[row1][column] = 0; //sets the row illegal
}
void set_diagonal()
{
int row, column;
for (row = row1 + 1, column = column1 + 1; row < N && column < N; row++, column++)
chess[row][column] = 0; //sets diagonals in the slope of -1 downwards illegal
for (row = row1 - 1, column = column1 - 1; row >= 0 && column >= 0; row--, column--)
chess[row][column] = 0; //sets diagonals in the slope of -1 upwards illegal
for (row = row1 - 1, column = column1 + 1; row >= 0 && column < N; row--, column++)
chess[row][column] = 0; //sets diagonals in the slope of +1 upwards illegal
for (row = row1 + 1, column = column1 - 1; row < N && column >= 0; row++, column--)
chess[row][column] = 0; //sets diagonals in the slope of +1 downwards illegal
}
After the change in bold, the only error I get is that the program doesnt actually work ahahaha. Anyways I'll figure that one out. Thanks for all the help!
void assign_q(int chess) - I think you want a board here, not a single field.
#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;
}