0
#include<stdio.h>
#define ROW 5
#define COL 5

int main(){
    int array[ROW][COL];
    int r=0, c=0;
    for (r=0; r<ROW; r++){
        for (c=0; c<COL; c++)

            array[c][r] = (c+1)-(r+1);

    }
    for (r=0; r<ROW; r++){
        for (c=0; c<COL; c++)
            printf("%5d", array[c][r]);
        printf("\n");
    }

    printf("\n\n");
    system("pause");
    return 0;
}

i need help in this code

I need help to modify the code to make it's output
from:
0 1 2 3 4
-1 0 1 2 3
-2 -1 0 1 2
-3 -2 -1 0 1
-4 -3 -2 -1 0

to:
0 1 1 1 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0

Edited by needhelpalways: wrong grammar

4
Contributors
11
Replies
13
Views
4 Years
Discussion Span
Last Post by Nutster
Featured Replies
  • 1

    Do you know how to write an `if` statement? There are three cases: if (number == 0) printf("0"); else if (number < 0) printf("-1"); else /* number > 0 */ printf("1"); Easy peasy. Read More

  • 1

    > you tell me this is the question given to me I still don't see any problem. By "manual assignment using initializer", your teacher means this: int matrix[5][5] = { { 0, 1, 1, 1, 1}, {-1, 0, 1, 1, 1}, {-1, -1, 0, 1, 1}, {-1, -1, -1, 0, … Read More

1

Do you know how to write an if statement? There are three cases:

if (number == 0)
    printf("0");
else if (number < 0)
    printf("-1");
else /* number > 0 */
    printf("1");

Easy peasy.

-1

@zeroliken good point, i'll try it

@deceptikon its due in like 5 hours dude.

Comments
Failure to explain why a given solution isn't suitable. I can't help if you don't explain.
0

@deceptikon its due in like 5 hours dude.

Whose fault is that? Yours. Please answer my question (why does the if statement solution not work?) if you want me to suggest other possible solutions. I'm not going to just list different ways of doing the same thing until you see one you like.

0

I notice you're exceptionally impatient. What's wrong with my answer?

My guess is obvious witty answers should be avoided :)

though too be honest when I saw the matrices, At first Glance they reminded me of reduced row echelon forms(though they're not the same) and using Gauss-Jordan Elimination and then I saw your post and it reminded me that simple problems have simple solutions... and that I read to much between the lines, took me a while to think things through

Edited by zeroliken

0

you tell me this is the question given to me
"Write a program that fills the left-to-right diagonal of a square matrix of order 5 (5x5 array) with zeros, the lower left triangle with -1s and the upper right triangle with 1s."

"Use nested loops in the assignment. Manual assignment using initializer is not allowed."

1

you tell me this is the question given to me

I still don't see any problem. By "manual assignment using initializer", your teacher means this:

int matrix[5][5] = {
    { 0,  1,  1,  1, 1},
    {-1,  0,  1,  1, 1},
    {-1, -1,  0,  1, 1},
    {-1, -1, -1,  0, 1},
    {-1, -1, -1, -1, 0},
};

Normalizing negative numbers to -1 and positive numbers to 1 strikes me as a completely viable solution. An if statement is clearly the simplest way to do that, though you could certainly use a clever expression to work out the normalization:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i;

    for (i = 10; i >= -10; --i) {
        if (i != 0)
            printf("%d\n", i / abs(i));
        else
            printf("%d\n", i);
    }

    return 0;
}

My answer wasn't intended to be "obviously witty". It's important to recognize simple solutions to simple problems and apply them when there's really no reason to be clever. Clever code is obtuse code, and obtuse code is difficult to maintain.

Edited by deceptikon

Comments
Agree :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.