Using Functions to Pass an Array - c

Hi, i am using a Function for the first time for my 2-D Array and so far i have been told it is right but i am having a problem with the 'For Loop'.
Specifically repeating the printed statement and numbers until the array is full, and also shows the specific array you are filling. (As the image shows).
This only prints 1 statement with 1 Array, whilst 4 numbers afterwards.. How could i get each number printed after each statement? I have tried the only way i know which made it worse.
Incorrectly Looped printed statement
Code below:
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++)
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}

You forgot to properly define the scope of the inner for loops.
The lines
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
are equivalent to:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
// Outside of both the for loops.
scanf("%d", &a[i][j]);
The indent expresses the intent to a human reader but not to the computer.
You need to use:
for (i=0; i<2; i++)
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
To make the code more readable add an explicit scope for the outer for loops too.
for (i=0; i<2; i++)
{ // Add
for(j=0; j<2; j++)
{ // Add
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
} // Add
} // Add
Make similar changes to the other loops.

Your for loops are probably not acting as you intended, because without the curly braces, they will only act on the first statement.
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
DisplayArray(a);
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d \n" , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==2)
printf("\n");
}
}
}

You're missing some curly braces. Change to this:
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
and
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d \n" , a[i][j]);
if(j==2)
printf("\n");
}
Also, note that if(j==2) will never evaluate to true. When j is equal to 2, the loop is over. You should change this to 1. I would also suggest to remove \n in the innermost printf statement.
Complete code:
#include <stdio.h>
void DisplayArray(int a[2][2]);
int main()
{
int a[2][2], i, j, k;
/*Counter variables for the loop*/
printf ("***** Functions ***** \n");
for (i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("\n Enter numeric values for each Array [%d] [%d]: \n", i, j);
scanf(" %d", &a[i][j]);
DisplayArray(a);
}
return 0;
}
void DisplayArray(int a[2][2])
{
int i, j;
/*Displaying Array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
for(j=0; j<2; j++) {
printf("%d " , a[i][j]);/*[i] name of 1 Array - [j] name of 2 Array*/
if(j==1)
printf("\n");
}
}

Related

Basically: Create a 2x2 multidimensional array using C that adds, subtracts, multiple and divides

I already finished creating the sum, difference, and product. What I am having trouble with is the quotient. because when I run it, It will just give me 0.0. I tried making the arrA and arrB a float which makes the quotient work but the others (sum, difference, and product) do not work. Please help me on how I could get the quotient to work.
#include <stdio.h>
int main()
{
int arrA[2][2];
int arrB[2][2];
float arrSum[2][2];
float arrDiff[2][2];
float arrProd[2][2];
float arrQuo[2][2];
int i,j;
//1st Matrix
printf("Enter Elements of 1st Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d", &arrA[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
//Second Matrix
printf("Enter Elements of 2nd Matrix\n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
printf("Enter b[%d][%d]: ",i,j);
scanf("%d", &arrB[i][j]); // to get value and save to array[][]
}
//printf("\n");
}
// Sum of Matrix
printf("Sum of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrSum[i][j] = arrA[i][j] + arrB[i][j];
printf("%.1f ", arrSum[i][j]);
}
printf("\n");
}
//Difference of Matrix
printf("Difference of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrDiff[i][j] = arrA[i][j] - arrB[i][j];
printf("%.1f ", arrDiff[i][j]);
}
printf("\n");
}
//Product of Matrix
printf("Product of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrProd[i][j] = arrA[i][j] * arrB[i][j];
printf("%.1f ", arrProd[i][j]);
}
printf("\n");
}
//Quotient of Matrix
printf("Quotient of Matrix: \n");
for (i=1; i<3; i++) // size of rows
{
for(j=1; j<3; j++) // size of columns
{
arrQuo[i][j] = arrA[i][j] / arrB[i][j];
printf("%.2f ", arrQuo[i][j]);
}
printf("\n");
}
return 0;
}
here is the result:
The output
here is what it should look like:
The supposed output

Values in array getting altered yet they shouldn't

It's a program that requires the user to enter the values of two 3 by 3 matrix then finds the sum of both matrices and prints out the same together with the added matrices but for some reason after entering the values of both matrices the a value in matrix gets altered then it affects the sum but at times it doesn't
#include<stdio.h>
void main(){
int matrix1[2][2];
int matrix2[2][2];
int sumMatrix[2][2];
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("Matrix[%d][%d]\n", i, j);
printf("Enter matrix one's values> ");
scanf("%d", &matrix1[i][j]);
}
printf("\n");
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("Matrix[%d][%d]\n", i, j);
printf("Enter matrix two's values> ");
scanf("%d", &matrix2[i][j]);
}
printf("\n");
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", matrix1[i][j]);
}
printf("\n");}
printf("\n");
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", matrix2[i][j]);
}
printf("\n");}
printf("\n");
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d ", sumMatrix[i][j]);
}
printf("\n");
}
}
You're declaring matrixes of size 2x2 and access them as if they were 3x3. What's happening more precisely is a buffer overflow, you write somewhere you shouldn't, and by doing so you overwrite other variables.
Lad, that is array overflow.
int matrix1[2][2];
But in the loop, you may get matrix1[2][1], matrix1[2][2].
Ok so clearly I'm the idiot. I thought array sizes are set based on the number of indices they'll have, so instead of int matrix1[2][2] it should be int matrix [3][3]

How can I transpose my Matrix to reverse columns and rows?

I managed to do enter M, N, and accept its condition (0<N and M<=10), then create and apply desirable number into 2 matrices, which is called Matrix A and Matrix B, then the Matrix C is the plus of 2 Matrix A B.
The last one is the transpose, I have looked upon some sites, but since the ways they do are not like mine so I don't like to just "straightly" copy and paste it.
Here is my code:
#include <stdio.h>
int main()
{
int M, N, i, j;
printf ("Enter number M: ");
scanf("%d", &M);
printf ("Enter number N: ");
scanf("%d", &N);
if (0 < N && M <= 10) {
printf ("Accepted number M: %d\n", M);
printf("Accepted number N: %d\n", N);
}
else printf("Please enter valid number, which is 0 < N and M <= 10!");
int matrixA[M][N], matrixB[M][N], matrixC[M][N];
printf ("Enter matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixA[i][j]);
}
}
printf ("Enter matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
scanf("%d", &matrixB[i][j]);
}
}
printf("Matrix A: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixA[i][j]);
}
printf("\n");
}
printf("Matrix B: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixB[i][j]);
}
printf("\n");
}
for (i=0; i<M; i++){
for (j=0; j<N; j++){
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
printf("Sum of matrix A and matrix B => Matrix C is: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", matrixC[i][j]);
}
printf("\n");
}
int reverseMatrixC[N][M];
for (i=0; i<M; i++){
for (j=0; j<N; j++){
reverseMatrixC[j][i] = matrixC[i][j];
}
}
printf("Reverse columns and rows in matrix C: \n");
for (i=0; i<M; i++){
for (j=0; j<N; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
}
The last one "Reverse columns and rows" are wrong and I don't know why, from my way of thinking, I'm switching M and N, then apply the previous Matrix C into a new variable which is reverseMatrixC, and then reverse i and j.
For example, I choose M = 2, N = 3.
and then I choose matrixA and matrixB these value:
1 2 3
4 5 6
The matrixC will display:
2 4 6
8 10 12
But when I transpose it, it'll display:
2 8 4
4 10 6
What I want is something like this:
2 8
4 10
6 12
You're very close. In fact, you're transposing matrixC correctly, the only problem is with how you're printing it out:
printf("Reverse columns and rows in matrix C: \n");
// reverseMatrixC has its rows and columns dimensions swapped, so N specifies its number of rows
for (i=0; i<N; i++){
// and M specifies its number of columns
for (j=0; j<M; j++){
printf("%d ", reverseMatrixC[i][j]);
}
printf("\n");
}
Also highly recommend to fix your indentation and use an IDE that formats for you, it makes reading code much easier.
Demo

Why this function is not showing the correct result as output?

The output of this function everytime is:
The row with minimal sum is 1 whose sum is 0.
But I want it to calculate the sum of each row and then display the min row with its sum.
It displays the correct result if I use this code as a program, and not inside the function(as in this case).
void findRowWithMinimalSum(int row, int col, int A[row][col]){
int i,j,sum,minSum,position;
for(i=0; i<row; i++){
printf("\nGive the elements of row %d:\n", i+1);
for(j=0; j<col; j++)
scanf("%d", &A[i][j]);
}
for(i=0; i<row; i++){
for(j=0; j<col; j++)
printf("%d", A[i][j]);
printf("\n");
}
for(i=0; i<row; i++){
sum=0;
for(j=0; j<col; j++){
sum = sum + A[i][j];
}
if(sum<minSum){
minSum = sum;
position = i;
}
}
printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
}
int main(){
int row, column, m[50][50];
//function call
findRowWithMinimalSum(3,3,m);
return 0;
}
Look carefully at minSum. You don't explicitly initialize it before using it in this test:
if (sum < minSum)
Therefore, if you're running a debug build and it gets initialized to zero, it'll never change unless sum is < 0.
Note that referencing an uninitialized value is undefined behavior, something that should be avoided at all costs.
Try this entering all numbers as negative.
Try setting it to INT_MAX and try again. Note that this is the correct solution to the problem.
As another editorial remark, it's probably not a good idea to modify the dimensions of the array. As declared in main() m is 50 by 50. But you're effectively viewing it as 3 by 3 in findRowWithMinimalSum(). That's not a good habit to get into, I can pretty much guarantee that doing so will cause problems for you at some point in the future.
Finally get yourself a debugger. This would have jumped right off the screen at you if you'd single stepped through findRowWithMinimalSum() watching how the variables change as the program progresses.
this will work
#include<stdio.h>
void findRowWithMinimalSum(int row, int col, int A[row][col]){
int i,j,sum,minSum,position;
for(i=0; i<row; i++){
printf("\nGive the elements of row %d:\n", i+1);
for(j=0; j<col; j++)
scanf("%d", &A[i][j]);
}
for(i=0; i<row; i++){
for(j=0; j<col; j++)
printf("%d", A[i][j]);
printf("\n");
}
for(i=0; i<row; i++){
sum=0;
for(j=0; j<col; j++){
sum = sum + A[i][j];
}
if(i==0){
minSum = sum;
position = i;
}
else if(sum<minSum){
minSum = sum;
position = i;
}
}
printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
}
int main(){
int row, column, m[50][50];
//function call
findRowWithMinimalSum(3,3,m);
return 0;
}
void findRowWithMinimalSum(int row, int col, int A[50][50])
{
int i,j,sum=0,minSum=0,position,f=0;
for(i=0; i<row; i++){
printf("\nGive the elements of row %d:\n", i+1);
for(j=0; j<col; j++)
scanf("%d", &A[i][j]);
}
for(i=0; i<row; i++){
for(j=0; j<col; j++)
printf("%d ", A[i][j]);
printf("\n");
}
for(i=0; i<row; i++){
sum=0;
for(j=0; j<col; j++){
sum = sum + A[i][j];
}
if(f==0)
{
minSum=sum;
f++;
}
if(sum<minSum){
minSum = sum;
position = i;
}
}
printf("\nThe row with minimal sum is %d whose sum is %d", position+1, minSum);
}
int main(){
int row, column, m[50][50];
//function call
indRowWithMinimalSum(3,3,m);
return 0;
}

Reading a 2-dimensional array of char in C

I'm trying to read an M*N array of chars, but I get some weird characters when printing the array. I couldn't figure out what's wrong with the code, what am I missing here?
int i,j,m,n;
char mat[10][10]
printf("N=");
scanf("%d", &n);
printf("M=");
scanf("%d", &m);
fflush(stdin);
// Read array elements, one by one
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i+1, j+1);
scanf("%c", &mat[i][j]);
fflush(stdin);
}
// Print matrix
putchar('\n');
for (i=0; i<n; i++);
{
for (j=0; j<m; j++)
printf("%2c", mat[i][j]);
putchar('\n');
}
Example input/output:
N=2
M=2
mat[1][1]=1
mat[1][2]=A
mat[2][1]=2
mat[2][2]=B
╥ ⌠
1
char mat[10][10] -> char mat[10][10];
for (i=0; i<n; i++); -> for (i=0; i<n; i++)
/* delete
for (i=0; i<(n-1); i++)
for (j=0; j<i+1; j++)
printf("%c ", mat[i][j]);
*/
your
scanf("%c", &mat[i][j]);
is catching the newlines. add white space at the beginning of the format
scanf(" %c", &mat[i][j]);
#include<stdio.h>
#include<string.h>
int main (void)
{
int i,j,m,n;
char mat[10][10];
char ch;
printf("N=");
scanf("%d", &n);
printf("M=");
scanf("%d", &m);
// Read array elements, one by one
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i, j);
//Flush standard inputs
while ((ch = fgetc(stdin)) != EOF && ch != '\n')
{
}
mat[i][j]=getchar();
printf("\n");
}
}
printf("printing %d x %d matrix", n , m);
// Print matrix
putchar('\n');
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("mat[%d][%d]=", i, j);
putchar(mat[i][j]);
printf("\n");
}
}
return (0);
}

Resources