Getting wrong output when adding elements in 2D Array - arrays

I am studying C programming now, I am not getting correct output when I am adding two elements in array, i am expecting your help to know the issue.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int limit = 0, sum[limit][3];
int a[limit][3];
int b[limit][3];
printf("Enter the size of arrays: \n");
fflush(stdout);
scanf("%d", &limit);
printf("Enter the values of Array 1");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Array 1: \n");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("Enter the values of Array 2");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
scanf("%d", &b[i][j]);
}
}
printf("Array 2: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
sum[i][j]= a[i][j] + b[i][j];
}
}
printf("Sum of 2 arrays: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return EXIT_SUCCESS;
}
Current output:
Enter the size of arrays:
3
Enter the values of Array 1
12
12
12
12
12
12
12
12
12
Array 1:
12 12 12
12 12 12
12 12 12
Enter the values of Array 2
11
11
11
11
11
11
11
11
11
Array 2:
11 11 11
11 11 11
11 11 11
Sum of 2 arrays:
22 22 22
22 22 22
22 22 22
The problem i found that the exact array i gave in code that doesn't workout.
My expected output is:
23 23 23
23 23 23
23 23 23

In:
int limit = 0, sum[limit][3];
int a[limit][3];
int b[limit][3];
limit is 0, your arrays will be:
int sum [0][3];
int a[0][3];
int b[0][3];
They will have space for nothing.
You should declare your arrays only after the limit input.
Also note that the second dimension of the array is fixed at 3, in your inner for cycles, instead of using limit you should use that constant value, otherwise, if limit is 4 or more, your program will access the array outside its bounds, invoking undefined behavior.

There are multiple problems:
the arrays are defined with a dimension set to 0:
int limit = 0, sum[limit][3];
int a[limit][3];
int b[limit][3];
you should define the arrays after you read the value og limit.
the nested loops use an incorrect boundary test:
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
j should iterate from 0 to 3 excluded.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int limit = 0;
printf("Enter the size of arrays: \n");
fflush(stdout);
if (scanf("%d", &limit) != 1 || limit < 1)
return 1;
int sum[limit][3];
int a[limit][3];
int b[limit][3];
printf("Enter the values of Array 1");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Array 1: \n");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("Enter the values of Array 2");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("Array 2: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of 2 arrays: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return EXIT_SUCCESS;
}

In the inner nested loops,j should iterate till j(excluding it) not limit.
Required nested loop should be like-
for (int i = 0; i < limit; i++) {
for (int j = 0; j < 3; j++) {
.
.
}
Tip:Declare your array after getting the input/size of the array from the user.

Related

How to add values to the first row and the first column of two dimensional array in C?

I am trying to add numbers from 0 to 9 in the row and 0 to 11 in column of a two dimensional array in C. And as for the rest of the empty spaces, I would like to add 0.
The matrix size is 9 x 11. And this is what the output looks like with the empty blocks filled with 0:
And this is the code I have so far but it does not work:
int i;
int j;
int arr[i][j];
int value = 0;
for (i = 0; i < 9; i++){
for (j = 0; j < 11; j++){
arr[i][j] = value;
printf("%d\n", arr[i][j]);
value++;
}
printf("\n");
}
The screenshot that you've posted has 10 rows and 12 columns, so assuming that, here is the code:
int i;
int j;
int arr[10][12];
for (i = 0; i < 10; i++) {
for (j = 0; j < 12; j++) {
if (i == 0) {
arr[0][j] = j;
} else if (j == 0) {
arr[i][0] = i;
} else {
arr[i][j] = 0;
}
printf("%d", arr[i][j]);
}
printf("\n");
}

How to find max of a matrix w/o knowing the size of it [duplicate]

This question already has answers here:
Passing a multidimensional variable length array to a function
(2 answers)
Closed 11 months ago.
the variable "students" is given by the user and then I have to use it in a function to find the max of a matrix. Note that "students" is the matrix's rows and I have to use it.
"students" NEEDS to be included in the function's arguments.
Also the better function isn't working properly and I cant figure out why.
These are the compiler messages:
warning: passing argument 1 of 'better' from incompatible pointer type [-Wincompatible-pointer-types]
73 | int max = better(*grades,students);
note: expected 'int **' but argument is of type 'int *'
5 | int better(int **pin,int students){
I am also getting a warning for comparing a pointer to an integer in function better()
My inputed numbers are 1 2 0 0 0 0 3 5 1 10 and the max that is returned is equal to 2.
#include <stdio.h>
#define TESTS 5
int better(int **pin,int students){
int max = 0;
int k = 0;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(*(pin + k) >= 0 && *(pin + k) <= 10){
if(max < *(pin + k)){
max = *(pin + k);
}
}
k++;
}
}
return max;
}
int main(){
int students;
printf("Grades should always be a number between 0-10!\n\n");
printf("Enter number of students: ");
scanf("%d", &students);
int grades[students][TESTS], value;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("Enter a value: ");
scanf("%d", &value);
if(value < 0){continue;}
if(value > 10){continue;}
grades[i][j] = value;
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(grades[i][j] < 0 || grades[i][j] > 10){
grades[i][j] = 0;
}
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("%d ", grades[i][j]);
}
printf("\n");
}
int max = better(*grades,students);
printf("\nmax of grades is: %d", max);
return 0;
}
I'd appreciate it if sb could help me!
#include <stdio.h>
#define TESTS 5
int better(int students, int grades[students][TESTS]){
int max = 0;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(max < grades[i][j]){
max = grades[i][j];
}
}
}
return max;
}
int main(){
int students;
printf("Grades should always be a number between 0-10!\n\n");
printf("Enter number of students: ");
scanf("%d", &students);
int grades[students][TESTS], value;
for(int i = 0; i < students; i++){
printf("Enter values for No.%d student:\n", i+1);
for(int j = 0; j < TESTS; j++){
printf("Enter a value: ");
scanf("%d", &value);
grades[i][j] = value;
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(grades[i][j] < 0 || grades[i][j] > 10){
grades[i][j] = 0;
}
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("%d ", grades[i][j]);
}
printf("\n");
}
int MAX = better(students, grades);
printf("Max grade is: %d", MAX);
return 0;
}

(SOLVED) C 2D Array: Finding Max from each row and Min from each column

I am creating a program to print out the maximum value of each row and the minimum value of each column of an NxN 2D array.
So, for example I have this 4x4 2D array:
1 14 11 16
9 10 3 12
13 6 15 8
13 2 7 4
Then, the output should be
16 12 15 13 //Max of each row
1 2 3 4 //Min of each column
EDIT: Here is the revised code that works.
#include <stdio.h>
#include <stdlib.h>
void getMax(int n, int a[][n])
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int n, int b[][n])
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(r,a);
printf("\n");
getMin(r,a);
printf("\n");
}
This is my OLD code that did not work. Turns out I don't have to use MAX. Replace the a[][MAX] to a[][n].
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void getMax(int a[][MAX], int n)
{
for (int i = 0; i < n; i++)
{
int max = a[i][0];
for (int j = 1; j < n; j++)
{
if (a[i][j] > max)
max = a[i][j];
}
printf("%d ",max);
}
}
void getMin(int b[][MAX], int n)
{
for (int i = 0; i < n; i++) {
int min = b[0][i];
for (int j = 1; j < n; j++)
{
if (b[j][i] < min)
min = b[j][i];
}
printf("%d ",min);
}
}
int main()
{
int r;
scanf("%d", &r);
int a[r][r];
for(int i=0; i<r; i++)
{
for(int j=0; j<r; j++)
{
scanf("%d",&a[i][j]);
}
}
getMax(a,r);
printf("\n");
getMin(a,r);
}
Can anybody point out what I did wrong?
Thank you!

Change column into row after a given number N

I need to change the matrix columns into rows after a given number N. For example if N = 3 then the row is n * 2 given in this exercise. Now after the 3rd column every other column needs to be a row. I know how to transpose a matrix but I get confused how to do it after given N.
Code:
#include <stdio.h>
int main() {
int n;
int a[n][n * 2];
int b[n * 2][n];
scanf("%d", &n);
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", a[i][j]);
}
}
for(int i = 0; i < n * 2; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = b[j][i];
}
}
return 0;
}
Example for n = 3.
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
I need to achieve
1 2 3
7 8 9
13 14 15
4 5 6
10 11 12
16 17 18
First square of the matrix (array) is left unchanged, so both arrays have the same starting. Later part is shifted n units left and n units down, so the following code should help you to solve the problem:
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i + n][j] = a[i][j + n];
}
}
As both set of for loops have the same kind of variable changes, we can converge them into one, as #Chris Turner did.
The whole code should look something like this:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n][n * 2];
int b[n * 2][n];
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
b[i + n][j] = a[i][j + n];
}
}
return 0;
}
Firstly you need to initialise n before you use it. In the code below, n has no defined value, so using it to define the size of your arrays leads to undefined behaviour.
int n;
int a[n][n * 2];
int b[n * 2][n];
You can move the array definitions until after you've set n
Secondly, when using scanf to read in an int you have to pass in a pointer to the variable, so this line is wrong and your compiler should be throwing up warnings about it
scanf("%d", a[i][j]);
it should be
scanf("%d", &a[i][j]);
if you want to copy from a into b you need to do it the right way around
you've got which is copying from b to a and also goes out of bounds as i goes up to n*2 and a's first index is just n.
a[i][j] = b[j][i]
what you want is just this.
b[i][j] = a[j][i]
But that doesn't solve your problem as that is just transposing and you're trying to split the matrix in half and stack it differently. So to start with you need to copy the first n by n elements from a to b like this.
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
and then to copy the second chunk you can use the same loops and offset by n
b[i+n][j] = a[i][j+n];
After all these changes your code should look like this:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n][n * 2];
int b[n * 2][n];
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
b[i+n][j] = a[i][j+n];
}
}
return 0;
}

How do I change the array Input Order in C

I need to input five numbers and then a positive or negative. The order will be changed like the number.
For example, if three (3) is the number then:
1 2 3 4 5 6 7 8 9 10
will become:
4 5 6 7 8 9 10 1 2 3
Please do not use pointers. I have made the following code and would like to be able to improve on it. Can I use a mathematical formula using the modulus operator % ? If so, how would I be able to do it.
Thanks, this is my code.
#include<stdio.h>
#define n 10
int main(void)
{
int num[n] = { 0 }, assist[n] = { 0 }, i = 0, j = 0, variable = 0, k = 0;
for (i = 0; i < n; i++)
{
printf("please enter index. %d\n", i );
scanf("%d", &num[i]);
}
printf("please enter the circle number\n");
scanf("%d", &variable);
printf("\n\n");
if (variable >= 0)
{
for (i = variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < variable, j < n; i++, j++)//to assist//
{
assist[j] = num[i];
}
}
if(variable < 0)
{
for (i = n + variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < n + variable, j < n; i++, j++)
{
assist[j] = num[i];
}
}
for (i = 0; i < n; i++)//output//
{
printf("%d\n", assist[i]);
}
return 0;
}
Something like this. I did not compile; any small mistakes are for you.
#include<stdio.h>
#define N 10
int main(void)
{
int numbers[N];
int offset;
for (int i = 0; i < N; i++)
{
printf("please enter number %d: ", i);
scanf("%d", &num[i]);
}
printf("\nplease enter the circle number: ");
scanf("%d", &offset);
printf("\n\n");
for (int i = 0; i < N; i++)
{
printf("%d ", num[(i + offset) % N]);
}
printf("\n");
}

Resources