Thread: another quicky :-)

  1. #1
    Unregistered
    Guest

    another quicky :-)

    #include<stdio.h>
    void main()
    {
    int PersonID,Wage,Sex,Age,n;
    int a[10][4];
    printf("please enter the amount of people you will use \n\r");
    scanf("%d",&n);
    for(PersonID=1;PersonID<=n;PersonID++);
    {
    printf("please enter the Person ID\n");
    scanf("%d",&a[PersonID][10]);
    return 0;
    }
    }




    I am trying to make the program loop and keep asking for values of person ID until N is reached i.e. user defines how many inputs
    to the array i.e. N ........ for some reason I can only enter 1 value for person id and not 10 if thats what I enter n as ............

    any ideas ???

    thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    scanf("%d",&a[PersonID][10]);

    should be

    scanf("%d",&a[PersonID-1 ][10]);

    you also have your return statement inside the loop. move it below the loop.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    nope don't help V_V

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<stdio.h> 
    int main() 
    { 
    int PersonID,Wage,Sex,Age,n; 
    int a[10][11]; 
    printf("please enter the amount of people you will use \n\r"); 
    scanf("%d",&n); 
    for(PersonID=1;PersonID<=n;PersonID++); 
    { 
    printf("please enter the Person ID\n"); 
    scanf("%d",&a[PersonID-1][10]); 
    } 
    return 0; 
    }
    try that.
    Last edited by Stoned_Coder; 11-25-2001 at 08:26 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    that should have fixed it but its beyond me why you want to store anything at the end of an array like that without using the rest of it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Unregistered
    Guest

    Talking

    what I am trying to do is create a table basically the array needs to store data for the for variables personid, wage, age, sex ...........

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > personid, wage, age, sex
    So
    PersonId is stored in a[i][0]
    Wage is stored in a[i][1]
    Age is stored in a[i][2]
    Sex is stored in a[i][3]

    If so, then
    scanf("%d",&a[PersonID-1][0]);

  8. #8
    Unregistered
    Guest
    so wage may equal

    scanf("%d",&a[Wage-2][10]);???????/

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > so wage may equal
    > scanf("%d",&a[Wage-2][10]);???????/
    No,

    scanf("%d",&a[PersonID-1][1]);

  10. #10
    Unregistered
    Guest

    hmmm

    I suppose u could say I am hopelessly confused hehe

    #include<stdio.h>
    int main()
    {
    int PersonID,Wage,Sex,Age,n;
    int a[10][4];
    printf("please enter the amount of people you will use \n\r");
    scanf("%d",&n);
    for(PersonID=1;PersonID<=n;PersonID++);
    {
    printf("please enter the Person ID\n");
    scanf("%d",&a[PersonID][10]);
    }
    for(Wage=1;Wage<=n;Wage++);
    {
    printf("please enter the Wage\n");
    scanf("%d",&a[Wage][10]);
    }
    for(Sex=1;Sex<=n;Sex++);
    {
    printf("please enter the Sex\n");
    scanf("%d",&a[Sex][10]);
    }
    for(Age=1;Age<=n;Age++);
    {
    printf("please enter the Age use 1 for male and 0 for female\n");
    scanf("%d",&a[Age][10]);
    }
    return 0;


    I am a newbie if u help please may u explain in real simple language thanks !

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do you listen to what you are told ?

    first off. arrays in c/c++ have there elements counted from zero. Thus if you say char array[10] the last element you can access is array[9].The first element being array[0] and not array[1]
    You can think of 2d arrays as an array of 1d arrays.Thus char array[3][3] has these addresses...
    array[0][0],array[0][1],array[0][2]
    array[1][0],array[1][1],array[1][2]
    array[2][0],array[2][1],array[2][2]

    secondly for loops dont have semicolons after them.Was wrecked earlier and didnt notice that before.

    starting to see why we needed that -1 in your indexing?

    Code:
    #include<stdio.h> 
    int main() 
    { 
    int PersonID,n; 
    int a[10][4]; 
    printf("please enter the amount of people you will use \n\r"); 
    scanf("%d",&n); 
    for(PersonID=1;PersonID<=n;PersonID++) 
    { 
    printf("please enter the Person ID\n"); 
    scanf("%d",&a[PersonID-1][0]); 
    } 
    // at this point a[PersonID-1][0] holds the personid
    for(PersonID=1;PersonID<=n;PersonID++) 
    {
    printf("please enter the Wage\n");
    scanf("%d",&a[PersonID-1][1]);
    }
    // at this point a[PersonID-1][1] holds the wage information
    for(PersonID=1;PersonID<=n;PersonID++)
    {
    printf("please enter the Age\n");
    scanf("%d",&a[PersonID-1][2]);
    }
    //at this point a[PersonID-1][2] holds the age information
    
    return 0; 
    }
    of course it would have been easier to put it all inside one loop.Thats an exercise for you....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Unregistered
    Guest

    I changed it a bit :-P

    #include<stdio.h>

    int main()
    {
    int /* PersonID,Wage,Sex,Age, */ n, i;
    int a[10][4];
    printf("please enter the amount of people you will use \n");
    scanf("\n%d",&n);
    printf("\n=%d\n\n", n);
    for (i=0; i<n; i++)
    {
    printf("please enter the Person ID (person %d)\n", n);
    scanf("\n%d",&a[i][0]);
    printf("please enter the Wage (person %d)\n", n);
    scanf("\n%d",&a[i][1]);
    printf("please enter the Age (person %d)\n", n);
    scanf("\n%d",&a[i][2]);
    printf("please enter the Sex use 1 for male and 0 for female (person %d)\n", n);
    scanf("\n%d",&a[i][3]);
    }

    printf("Data held in array:\n");
    printf("~~~~~~~~~~~~~~~~~~~\n");
    for (i=0; i<n; i++)
    {
    printf("Person %d\n\n", i);
    printf("ID: %d\n", a[i][0]);
    printf("Wage: %d\n", a[i][1]);
    printf("Sex: %d\n", a[i][2]);
    printf("Age: %d\n\n", a[i][3]);
    }
    return 0;
    }


    think this is better ????

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Much better - just a couple of formatting funnies to sort out, and I think you're done

  14. #14
    Unregistered
    Guest
    where can I learn how to sort data ???

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a quicky C reference guide of come sort
    By iecki in forum C Programming
    Replies: 2
    Last Post: 01-17-2008, 06:42 PM
  2. Quicky Question
    By Krak in forum C++ Programming
    Replies: 8
    Last Post: 02-06-2005, 05:45 AM
  3. quicky
    By CobraCC in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2003, 07:49 PM
  4. stderr quicky question - I GOT IT thank you so much
    By HOWY in forum Linux Programming
    Replies: 0
    Last Post: 07-05-2002, 10:28 AM
  5. stderr quicky question
    By HOWY in forum Linux Programming
    Replies: 1
    Last Post: 07-05-2002, 09:52 AM