problem in multi times table

This is a discussion on problem in multi times table within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int multi[10][3]; int a,b; int starter; int number; fputs("Please enter the number you want: ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    problem in multi times table

    Code:
    #include <stdio.h>
    int main(void)
    {
    int multi[10][3];
    int a,b;
    int starter;
    int number;
             fputs("Please enter the number you want: ",stdout);
             scanf("%d",&starter);
             fputs("Please enter number of numbers: ",stdout);
             scanf("%d", &number);
    		 for(a=starter;a<=number;a++){
    						 for(b=0;b<=3;b++){
    multi[a][b]= (a+1) * (b+1);
    						 }
    		 }
    for(a=starter;a<=number;a++)
    {
    for(b=0;b<=3;b++);
    printf("%d * %d= %d",multi[10][3]);
    puts("");
    }
    return 0;
    }
    it just printed out the not correct numbers i tried to find the error but couldnt .

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    You do not check starter and number values. May point out of array.
    Loops for arrays should stop one step before maximum size. (If b becomes 3 then you point to nowhere, the same to the rest loops)

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    What is your program suppose to do? Can you describe it better?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yes i wanna make a times table
    but it suppose to from 0 to 10 but it didnt it print some weird numbers

  5. #5
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Code:
    for(a=starter;a<=number;a++)
    {
    for(b=0;b<=3;b++);
    printf("%d * %d= %d",multi[10][3]);
    puts("");
    }
    You always type the same. I suppose you would like to write

    Code:
    for(a=starter;a<number;a++)
    {
    for(b=0;b<3;b++);
    printf("%d * %d= %d",a+1,b+1,multi[a][b]);
    puts("");
    }

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    dam i keep forgetting that anyways thanks

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    also fixed it
    Code:
    #include <stdio.h>
    int main(void)
    {
    int grid[20][20];
    int i,j;
    int start,numberofnumbers;
    fputs("Please Enter the number u wanna start with: ",stdout);
    scanf("%d",&start);
    fputs("Please enter the number of numbers: ",stdout);
    scanf("%d",&numberofnumbers);
    for(i=start;i<numberofnumbers;i=i+1)
    for(j=0;j<20;j=j+1)
    grid[i][j]=(i+1) * (j+1);
    for(i=start;i<numberofnumbers;i=i+1){
    for(j=0;j<20;j=j+1)
    printf("%3d ",grid[i][j]);
    puts("");
    }
    return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    You might want to indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah i still a begineer but i should work on that yah :S

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    why when i do to the for loop {
    after the for loop it print the number on a line itself without braceses it
    print it normal ?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by lolguy
    why when i do to the for loop {
    after the for loop it print the number on a line itself without braceses it
    print it normal ?
    Without the braces the scope of the for loop is the next statement.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by lolguy View Post
    also fixed it
    Code:
    #include <stdio.h>
    int main(void)
    {
    int grid[20][20];
    int i,j;
    int start,numberofnumbers;
    fputs("Please Enter the number u wanna start with: ",stdout);
    scanf("%d",&start);
    fputs("Please enter the number of numbers: ",stdout);
    scanf("%d",&numberofnumbers);
    for(i=start;i<numberofnumbers;i=i+1)
    for(j=0;j<20;j=j+1)
    grid[i][j]=(i+1) * (j+1);
    for(i=start;i<numberofnumbers;i=i+1){
    for(j=0;j<20;j=j+1)
    printf("%3d ",grid[i][j]);
    puts("");
    }
    return 0;
    }
    If all you want to do is output the grid of numbers, then you don't actually need to calculate and store all of the answers. Just output the answers as you go.

    Learning to program involves a lot more than simply producing the correct output though. You need to learn about whitespace. Right now your code has about as much whitespace as a Matrix screensaver, and it's gone from bad to worse.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. A Table Problem
    By kasun in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2004, 11:53 AM
  4. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 12:45 PM
  5. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 07:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21