#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

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.

initializing 0, 1, -1 is not allowed

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

how about dividing each element by the positive value of itself (other than the zeros)

@zeroliken good point, i'll try it

@deceptikon its due in like 5 hours dude.

commented: Failure to explain why a given solution isn't suitable. I can't help if you don't explain. -2

@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.

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

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."

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.

commented: Agree :) +9

ok dude i'm almost finish with it now
so yeah thanks you two

Speaking as a teacher, obtuse code is also a pain to mark, let alone maintain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.