KingSwoozie
KingSwoozie

Reputation: 39

No output in c program?

I made a 2 dimension array where I have to type 16 numbers and the program should return me the biggest value and its location (This is just an exercise, I know I can just use an array with 16 values instead of 4x4)

#include <stdlib.h>
#define CONSTC 4
#define CONSTL 4
 int main ()
 {
     int i=1;
     int e;
     int k;
     int c;
     int a;

        int A [CONSTR][CONSTC];

    for (k=0; k<CONSTL; k++)
    {
        for (c=0; c<CONSTC; c++, i++)
        {
                printf ("%2d - Type number\n", i);
                scanf ("%d", &A [k][c]);
        }
    }

    e = Matriz [0][0];

    for (k=0; k<CONSTL; k++)
    {
        for (c=0; c<CONSTC; c++, i++)
        {
               if (e>A[k][c])
               {
                e = A [k][c];
                return k;
                return c;
               }
        }
    }
    printf ("\t\tBiggest Value = %d\nValue Location = %d %d", e, k, c);

 }

Upvotes: 0

Views: 115

Answers (3)

jeremynac
jeremynac

Reputation: 1242

I corrected some stuff in your code

#include <stdlib.h>
#include <stdio.h> //you need to include this to use printf and scanf
#define CONSTC 4
#define CONSTL 4

int main()
{
     int i=1;
     int e;
     int k;
     int c;
     int a;

        int A [CONSTL][CONSTC];

    for (k=0; k<CONSTL; k++)
    {
        for (c=0; c<CONSTC; c++, i++)
        {
                printf ("%2d - Type number\n", i);
                scanf ("%d", &A [k][c]);
        }
    }

    e = A[0][0];//Not sure what Matriz was, you meant A right?

    for (k=0; k<CONSTL; k++)
    {
        for (c=0; c<CONSTC; c++, i++)
        {
               if (e>A[k][c])
               {
                e = A [k][c];
                //return k;
                //return c;
               }
        }
    }
    printf ("\t\tBiggest Value = %d\nValue Location = %d %d", e, k, c);
    return 0;
 }

The main problem was that you let return k and return c in the middle of the loop, return stops the whole process and the whole program.

So you never get to the last printf.

Also, if you need the biggest value, you would need your if statement to be if(e < A[k][c]).

Upvotes: 3

Rohan Bari
Rohan Bari

Reputation: 7736

There are several problems in the code:

  1. The return statement would return the given code to the main(), which makes no sense.

  2. Some variables aren't declared which is a huge error.

  3. Defined stdlib.h but I/O operations does exists in stdio.h library.

  4. After the first return statement, the second will never get executed.


Code redefined:

#include <stdio.h>

#define MAX_ROWS 4
#define MAX_COLS 4

int main(void) {
    int mat[MAX_ROWS][MAX_COLS];
    int max = 0;
    int posM, posN;

    printf("Enter [4 x 4] Matrix below: \n");

    for (int i = 0; i < MAX_ROWS; i++)
        for (int j = 0; j < MAX_COLS; j++)
            scanf("%d", &mat[i][j]); // getting the values of i * j positon

    for (int i = 0; i < MAX_ROWS; i++)
        for (int j = 0; j < MAX_COLS; j++)
            if (mat[i][j] > max) {
                max = mat[i][j]; // if max is lesser than the current one, then assign
                posM = i + 1; // we had started the counting from 0, so increased 1
                posN = j + 1;
            }

    printf("The largest element in the matrix: %d\n", max);
    printf("The position is: %d x %d\n", posM, posN); // simply get the value

    return 0;
}

The program simply asks the user to fill 4 ✕ 4 multidimensional array values. And then a loop is executed once as soon as the first loop quits.

The second loop tries to find the maximum value held by the matrix array. If the current value is lesser than the previously scanned value, it's then assigned. This process continues until the loop ends and finally displays the results.


A sample output:

Enter [4 x 4] Matrix below: // --- INPUT
1 2 3 4
4 3 2 1
3 3 3 4
5 4 3 1
The largest element in the matrix: 5 // --- OUTPUT
The position is: 4 x 1

Upvotes: 1

The return; statement ends the function. When there's an expression afterwards, e.g. return k;, the result of the function is k.

Since the return statement exits from the function main, no more code runs after that point. This is why no output is printed.

Upvotes: 0

Related Questions