Copying each row of matrix to a temporary array - c

I want to copy a row (one each team iterating) of a 500x8 matrix to a temp array with the name actual_row. This is what I've tried.
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
// do other stuff
}
}
printf("\n");
}
This is not printing the line, it's printing 0's and 1's sometime, so there's something I'm doing wrong.
Thanks in advance.

Don't print actual_row before it's filled completely:
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
}
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
...
}

Your logic is slightly off. You need to copy the row to actual_row, then print the contents. Furthermore, why not just print the contents while you are copying the matrix row to actual_row:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
So your code snippet should be this:
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
// <--at this point, actual_row fully contains your row
printf("\n");
}

Your logic is slightly off (no need for the third nested loop). You need to copy the row to actual_row (which you did), and print the contents within the same loop:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}

Related

How to transpose a matrix without using another matrix?

Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.

How to get access to each row and column in rectangular matrix

Good day to everybody. My task is to determine if a rectangular matrix has two rows of positive elements. I write the code below. At end I try to chect statement about positive row, but it's not working at all. Please explain me how to correct get the access to the each row and column in matrix, and meaybe edit my code.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}
When counting the number of positive elements in a row, you need to set the count back to zero for each row.
So instead of:
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
you need
for (i = 0; i < M; i++){
count = 0;
for (j = 0; j < N; j++) {
if (a[i][j] > 0){
count++;
}
}
printf("row %d has %d positive elements\n", i, count);
}

Printing Two C Columns One Sorted

I'm working on taking 10 numbers as user input and having the input printed in two columns with one sorted column.
#include <stdio.h>
int main(void)
{
int NUM_ELEMENTS = 10;
int list[NUM_ELEMENTS];
int sortedList[NUM_ELEMENTS];
int i, j, temp;
printf("Enter 10 numbers:\n");
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];}
}
}
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) {
temp = list[i];
sortedList[j] = temp;
}
}
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]);
}
return 0;
}
The first column prints correctly but the second column doesn't print the correct numbers. I'm fairly new at coding in C and can't seem to get this to work right. I've tried several variations of the provided code. The columns are also supposed to be side by side and labeled as well.
There are lot of bugs in the above code.
-firstly, looping constraints are wrong in scanning and assigning part
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];} /* for i=0 why list[0] assigned to all sortedList[j] ?
}
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d", &list[i]);
sortedList[i] = list[i];
}
-Secondly,sorting logic is not correct at all.
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) { /* what is sortedList[j] ? */
temp = list[i];
sortedList[j] = temp;
}
}
Replace above one with simple sorting technique logic for now but once become strong with basics check performance view also, which sorting method is better to use.
for(i = 0; i < NUM_ELEMENTS-1; i++) {
for(j = 0;j < NUM_ELEMENTS-1-i; j++) {
if(sortedList[j] > sortedList[j+1]) {
temp = sortedList[j];
sortedList[j] = sortedList[j+1];/* you miss this */
sortedList[j+1] = temp;
}
}
}
-Finally, there is bug in print part also.
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]); /* what is j here ? you can use the same variable i for both. */
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[i]);
}

#EMERGENCY!!! Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted

I know this seems like an old question, but none answered questions I searched work.
I have kept receiving "Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted." when I was trying to do a [4][2]*[2][3] matrix multiplication.
Does anyone spot the problem?
#include <stdio.h>
int main() {
int a[4][2] = {0};
int b[2][3] = {0};
int c[3][3] = {0};
int i, j;
printf("Please enter first matrix value\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Please enter second matrix value\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
printf("\n the result is :\n");//
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
printf(" %4d ", c[i][j]);
}
printf("]\n");
}
return 0;
}
I haven't checked your code thoroughly, but you define c as 3x3, and here
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
...you access c[3], which is c's fourth element, and does not exist. This is bound to write somewhere else.
So check your indexes (as #ptb observed, c's should actually be four rows deep).

C language copying arrays - error in code

I am trying to copy elements from two arrays into a third.
I can't understand why it doesn't work.
I made sure that the two arrays are filled properly, but for some reason, the actual copying doesn't work- when I print the elements of the arr3, I get some random numbers.
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n;
printf("Enter a number of elements to be stored in each array (up to 10): ");
scanf("%d", &n);
printf("Enter the %d elements to the first array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
printf("Enter the %d elements to the second array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
/*
// A test to make sure first 2 array are filled by the user- works
for(i = 0; i < n; i++)
printf("%d ", arr1[i]);
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
*/
// something wrong here, the elements are not coppied to the third array
for(i = 0; i < n; i++);
arr3[i] = arr1[i];
for(i = n; i < 2 * n; i++)
arr3[i] = arr2[i];
for(i = 0; i < 2 * n; i++)
printf("%d\n", arr3[i]);
return(0);
}
You're reading past the end of arr2, try this;
for (i = 0; i < n; i++)
arr3[i] = arr1[i];
for (i = 0; i < n; i++)
arr3[n+i] = arr2[i];
That's because your code is reading arr2[10],arr2[11] ..... arr2[19] (if n=10 ), all these values do not exist because arr2 only has 10 values. you can use this.
for (i=0; i<n; i++)
arr3[n+i]=arr2[i];
or
for (i=n; i<n*2; i++)
arr3[i]=arr2[i-n];

Resources