Thread: problem in multi times table

  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
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    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
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    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
    28,412
    You might want to indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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, 05: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, 06:03 AM