Skip to main content
gamedev.net
🔒 Locked

Basic 2D array C problem

Started by Blackdoor_dev May 6, 2015 at 5:22 AM 25 replies 5.2k views
Original Post
Blackdoor_dev
Blackdoor_dev

What am I doing wrong?

//Multiplication table using 2D array
#include
#include
main()
{
int mul[5][5];
int row,column;
int i,j;
clrscr();
printf("Here is the multiplication table \n ");
for(i=0;i<5;i++);
{
row=i+1;
for(j=0;j<5;j++)
{
column=j+1;
mul[j]=row*column;
printf("%5d",mul[j]);
}
printf("\n");
}
getch();
return 0;
}
Ashaman73
Ashaman73

What am I doing wrong?

Well, you don't ask the right question and/or give little to no information about your issues.

Your code looks okay at first glance, so it would be really helpful if you have an issue and what the issue is ? Does it not compile ? Not run ? Crash ? Produces not expected result ? What results are you expecting ? ...

MarkS_
MarkS_

Well.... I don't know. What is it doing? What were you expecting it to do? It looks OK, but I'm not going to compile your code to figure out the issue.

Blackdoor_dev
Blackdoor_dev

What am I doing wrong?

Well, you don't ask the right question and/or give little to no information about your issues.

Your code looks okay at first glance, so it would be really helpful if you have an issue and what the issue is ? Does it not compile ? Not run ? Crash ? Produces not expected result ? What results are you expecting ? ...

I want to find the multiplication table of 5 numbers

It is compiling but giving me number 6 table only

MarkS_
MarkS_

Not with the code you posted, it isn't...

Just out of curiosity, this wouldn't happen to be a homework problem, would it?

Blackdoor_dev
Blackdoor_dev

Not with the code you posted, it isn't...

Just out of curiosity, this wouldn't happen to be a homework problem, would it?

Nope.

MarkS_
MarkS_
This line right here is a problem:

for(i=0;i<5;i++);


Remove the semicolon at the end of the for statement. This is a do nothing loop. It increments 'i' to 4 and continues.
MarkS_
MarkS_
It's these little mistakes that get you. I couldn't even see it at first.
MarkS_
MarkS_
I'm curious why you are using an array at all. This is much cleaner:


#include<stdio.h>
#include<conio.h>
main()
{
    int i,j;  
    clrscr();
    printf("Here is the multiplication table \n ");
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            int product = (i + 1) * (j + 1);
            printf("%5d",product);
        }
        printf("\n");
    }
    getch();
    return 0;
}
ferrous
ferrous

Also a case where walking through with a debugger would've made it immediately apparent.

MarkS_
MarkS_
I'm still convinced this is a school project:

"Using a two dimensional array, compute the multiplication table from 1 to 25 and display in matrix form." Or something along those lines...
Blackdoor_dev
Blackdoor_dev

I'm still convinced this is a school project:

"Using a two dimensional array, compute the multiplication table from 1 to 25 and display in matrix form." Or something along those lines...

Sorry bro couldn't reply to you earlier. :c

I am actually learning C Array(Right now) from a Book.In which it was asking me to to solve it through array like you said.

I am not in school.

Next I have to learn array with String and char then Functions and passing of array and strings,,,then union and struct then IO file management and then dynamic memory and then etc etc and then C and C++ and then ill start using Unreal engine 4.After learning little bit about graphics.

MarkS_
MarkS_
Good to know!

Arrays are used to store data. The way you wrote the first code snippet really didn't use the array. Fill the array and THEN use the stored data. Don't fill the array AND use the stored data. It's a waste of memory in that case, unless the array will be used further on.

Note that I got rid of "row" and "column". They are redundant. In a more complicated example, additional variables would help to make the code clearer. In this case, they add unnecessary verbiage. It doesn't hurt to use them, but it is just more typing for the same effect.

This would make more sense:

//Multiplication table using 2D array

#include<stdio.h>
#include<conio.h>

main()
{
    int mul[5][5];
    int i,j;

    clrscr();
    printf("Here is the multiplication table \n ");

    // Fill the multiplication table array.
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            mul[i][j]= (i + 1) * (j + 1);
        }
    }

    // Here you might do operations on the array data....

    // Print the array.
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("%5d",mul[i][j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}
You can also unroll the loops:

//Multiplication table using 2D array

#include<stdio.h>
#include<conio.h>

main()
{
    int mul[5][5];
    int i;

    clrscr();
    printf("Here is the multiplication table \n ");

    // Fill the multiplication table array. Unroll and eliminate the 'j' loop.
    for(i=0;i<5;i++)
    {
        mul[i][0]= (i + 1);
        mul[i][1]= (i + 1) * 2;
        mul[i][2]= (i + 1) * 3;
        mul[i][3]= (i + 1) * 4;
        mul[i][4]= (i + 1) * 5;
    }

    // Here you might do operations on the array data....

    // Print the array.
    for(i=0;i<5;i++)
    {
        printf("%5d%5d%5d%5d%5d\n",mul[i][0],mul[i][1],mul[i][2],mul[i][3],mul[i][4]);
    }
    getch();
    return 0;
}
Or you can use a one dimensional array as a two dimensional array:

//Multiplication table using 2D array

#include<stdio.h>
#include<conio.h>

main()
{
    int mul[25];
    int i,j;

    clrscr();
    printf("Here is the multiplication table \n ");

    // Fill the multiplication table array.
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            mul[(i * 5) + j]= (i + 1) * (j + 1);
        }
    }

    // Here you might do operations on the array data....

    // Print the array.
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("%5d",mul[(i * 5) + j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}
Or this... Now I'm just having fun! biggrin.png

//Multiplication table using 2D array

#include<stdio.h>
#include<conio.h>

main()
{
    int mul[5][5];
    int *table_ptr;
    int i,j;

    printf("Here is the multiplication table \n ");

    // Fill the multiplication table array.
    for(i=0;i<5;i++)
    {
	table_ptr = &mul[i][0];

        *table_ptr++ = (i + 1) * 5;
        *table_ptr++ = (i + 1) * 4;
        *table_ptr++ = (i + 1) * 3;
        *table_ptr++ = (i + 1) * 2;
        *table_ptr = (i + 1);
    }

    // Here you might do operations on the array data....

    // Print the array.
    for(i=0;i<5;i++)
    {
	table_ptr = &mul[i][0];
        printf("%5d%5d%5d%5d%5d\n",*table_ptr,*table_ptr++,*table_ptr++,*table_ptr++,*table_ptr++);
    }
    getch();
    return 0;
}
L. Spiro
L. Spiro

and then C and C++

Why not just C++?


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
MarkS_
MarkS_

Why not just C++?


^^ This!

If I could go back and do it over, I'd skip pure C and jump straight to C++. It has been a difficult transition for me.
Blackdoor_dev
Blackdoor_dev

Why not just C++?


^^ This!

If I could go back and do it over, I'd skip pure C and jump straight to C++. It has been a difficult transition for me.

and then C and C++

Why not just C++?


L. Spiro

Well I finished school back in 2013.I was a very good student in school.Even got 96% in board's final which is rare.I joined a college to study BCA(Bachelor of Computer Application) and then wanted to study MCA.
But after 1 months of college I was struck by a disease(let's say disease X).From that day still now I wasn't able to visit college or do anything.This disease ruined my life kinda.
Even I tried to go to the college in between but again failed and I end up wasting my parent's money(they are supportive and in a financial crisis).Maybe I won't able to study in future too.My parents are the best mom and dad of this world with the worst luck of having me.30 days back I thought about not giving up and studying by myself as much as I can and not letting that disease X to win.I started from the books that i got from my college.They were teaching us C,Digital electronics,IT and maths back then.So I started with C.Now I like gaming and everything related to game and I also want to create or together with a team.So i joined this forum.It is really hard to study by myself.From student life I always tend to go to the absolute depth of that when studying.Which is hard to maintain now. :c
Thanks for the help guys...And I noted ur point MarkS smile.png

MarkS_
MarkS_

I started out with C back in the early 90's. The first compiler I had was Think C on the Mac. It was a straight C compiler with absolutely no C++ features. I learned the bulk of my programming habits and techniques back then. When I was finally exposed to C++ and OOP, C was so strongly ingrained into my way of thinking that C++ was like looking at old English. Sure, I could read it, but God help me understand it! As a result, I blew off C++ for more than a decade.

There is little reason to learn C. The programming community, by and large, has moved away from it and its principles. Learning C will bog you down in a mire that will be hard to pull yourself out of. The C concepts that are still in use in C++ will be learned when learning C++. The two stalwarts of C development today are operating system development and embedded systems development and both of those are transitioning away.

If I could do it over, I would have never learned C. I would have went directly to C++.

Blackdoor_dev
Blackdoor_dev

I started out with C back in the early 90's. The first compiler I had was Think C on the Mac. It was a straight C compiler with absolutely no C++ features. I learned the bulk of my programming habits and techniques back then. When I was finally exposed to C++ and OOP, C was so strongly ingrained into my way of thinking that C++ was like looking at old English. Sure, I could read it, but God help me understand it! As a result, I blew off C++ for more than a decade.

There is little reason to learn C. The programming community, by and large, has moved away from it and its principles. Learning C will bog you down in a mire that will be hard to pull yourself out of. The C concepts that are still in use in C++ will be learned when learning C++. The two stalwarts of C development today are operating system development and embedded systems development and both of those are transitioning away.

If I could do it over, I would have never learned C. I would have went directly to C++.

Ugh brah.
I am too much into it right now I finished array now at string which is an array xD
As C doesn't support String data type.If in future i go to college then ill need it too :c
But i got what u are trying to say...I really hope I can adapt to the change in C++

SOL-2517
SOL-2517

I think he's exaggerating a bit. I learnt C first and I had no trouble learning C++ afterward. I don't recommend either one of them as your first programming language though but that's another issue. I still use C instead of C++ a lot of the time depending on what needs to be done. As long as you don't become fanatical about one particular language or a way of doing things then you will be fine.




When I was finally exposed to C++ and OOP, C was so strongly ingrained into my way of thinking that C++ was like looking at old English.

That's funny because I've heard the same thing from people who have tried to move away from OOP to a data-oriented approach.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.