Thread: writing a pair of numbers to an array

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh wait... I should have read instead of assumed. struct coordinates is a type name. The struct object of that type is named number1. As such, you should write:
    Code:
    read_location(&number1.x, &number1.y);
    By the way, number1 should be a local variable instead.
    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

  2. #17
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    Oh wait... I should have read instead of assumed. struct coordinates is a type name. The struct object of that type is named number1. As such, you should write:
    Code:
    read_location(&number1.x, &number1.y);
    By the way, number1 should be a local variable instead.
    Thanks for the clarification laserlight. As for the number1, do you mean I should move it into the read_location function?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wankel
    Thanks for the clarification laserlight. As for the number1, do you mean I should move it into the read_location function?
    No, into the main function.
    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

  4. #19
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    No, into the main function.
    im assuming you were referring to the "struct coordinates number1". in that case, i took your advice and moved it into main. Thanks

  5. #20
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Also would

    Code:
    struct coordinates array[];
    be the best way to store the values into an array? I was thinking about putting this in a loop that will keep adding new pairs to the array

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your array needs a size. You can't just declare an empty and expect it to hold an unlimited amount or whatever it is you're thinking it's going to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by quzah View Post
    Your array needs a size. You can't just declare an empty and expect it to hold an unlimited amount or whatever it is you're thinking it's going to do.


    Quzah.
    So Im initializing an array of size 20 to zero as follows.
    Code:
    int array[19]={0};
    Then Im using this to create the array of structs:
    Code:
    Coordinates array[19];
    and then to access the elements, im using the following:
    Code:
    array[i].x
    array[i].y
    This is what I understood with some googling. Would it work?
    Last edited by wankel; 06-21-2009 at 12:25 PM.

  8. #23
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well no. Here is something which could help

    1. Define a structure which hold your two integer values
    Code:
    struct node
    {
        int num1;
        int num2;
    };
    2. Declare an array of that struct.
    Code:
    struct node array[20];
    3. And then read value into it.
    Code:
    while( index < 20 )
    {
        read_location( &array[index].num1, &array[index].num2 ) or
        read_location( &array[index] )
        index++;
    }
    Thats 3rd step would depend on how you defined your read_location function.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #24
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by ssharish2005 View Post
    Well no. Here is something which could help

    1. Define a structure which hold your two integer values
    Code:
    struct node
    {
        int num1;
        int num2;
    };
    2. Declare an array of that struct.
    Code:
    struct node array[20];
    3. And then read value into it.
    Code:
    while( index < 20 )
    {
        read_location( &array[index].num1, &array[index].num2 ) or
        read_location( &array[index] )
        index++;
    }
    Thats 3rd step would depend on how you defined your read_location function.

    -ssharish
    Thanks for the good advice, how can I test to see if the values stored are accurate before i continue with my code? I have this line following the loop to see if the 4th element is what i told it to be:

    Code:
    printf("%d", &array[3].x);
    but this keeps displaying random numbers.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    calls to the printf() function should not include the ampersand in them:

    Remove the &.

  11. #26
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    calls to the printf() function should not include the ampersand in them:

    Remove the &.
    I forgot to say, if I have it without the "&", it just displays a "1" every time, no matter what number i choose to enter.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Printf() calls, never include the ampersand - unless you want to print the address of the variable, in memory.

    Your incorrect result is caused by other problems in your code. Why don't you print your latest full program, and i'll take a look at it?

  13. #28
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    I rewrote the program since I lost track of what I was doing the first time, heres what i have now:

    Code:
    #include <stdio.h>
    
    struct coord{
    	double x;
    	double y;
    };
    
    struct coord array[20];
    
    int main ()
    {
    	int index=0;
    	do
    	{
    		read_location( &array[index].x, &array[index].y);
    		index++;
    	}while(index < 5);
    	
    	printf("%d", array[3].x);
    	return 0;
    }
    
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", &x);
    	printf("Enter y:\n");
    	scanf("%lf", &y);
    }
    Now im thinking the issue is in the "read_location" function. Thanks for the help, its much appreciated

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll be back, shortly.

    What compiler are you using?

    Change %d in printf() to a double format.

    your void function should be void, so remove the int return type. Add a prototype for the function, above main().

    I've got to look up something about doubles, grrrrr!
    Last edited by Adak; 06-21-2009 at 02:36 PM.

  15. #30
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Declare your struct in the main function. There is scope problem on your code. The main function would always return 0 rather than the value you store then in the function.

    And your read_location function should look like this

    Code:
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", x);
    	printf("Enter y:\n");
    	scanf("%lf", y);
    }
    Take of the & from the scanf. Thats becaue the you need to give the address of variable x & y which is sent from the main. when say &x in the function it getting the address of pointer x and y which is not what you want.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Writing to my 2D Array
    By qwertysingh in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 01:16 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM