Thread: Read in values to Array

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    Read in values to Array

    Hey, I'm working on a project for school where I have to do a simple linear algebra calculator.
    I've got most of it thought out and written but I can't seem to get a funciton to read in values to a 'vector' 1Darray ...
    this is the function I wrote, obviously wrong...

    Code:
    /**********************************************************************
     read_vector():
     This function will read in a vector of size n into the array v.  It  
     will prompt the user to enter n elements, then it will read in the  
     elements, one at a time.  The elements entered will only be separated  
     by white space, but they can span over multiple lines or be on the  
     same line. 
     **********************************************************************/
    void read_vector(int *v, int n)
    {
    	int i = 0, value = 0;
    	printf("Vector value: ");
    	while ( i < n)
    	{
    		scanf("%d", value);
    		*v++ = value;
    		i++;
    	}
    	
    }

    any help would be appreciated. My problem is how I can use scanf 'n' number of times...

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Read Cprogramming.com FAQ > How do I get a number from the user (C), and I suggest to change
    Code:
    *v++ = value;
    to
    Code:
    v[i] = value;

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    I think it does the same thing, since it increments after it performs the assignment... it doesn't really change anything that fixes my problem... :/
    i get a bus error every time I call that function..

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Code:
    scanf("%d", value);
    perhaps replace with

    Code:
    scanf("%d", &value);

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by fcommisso View Post
    it doesn't really change anything that fixes my problem
    I know it doesnt, which is why I first provided the link (which fixes your problem), then I just gave a suggestion about something that doesnt solve your problem.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    oh thanks, didn't see the link. thanks a lot.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Ok
    so I tried to follow what the link you sent me was describing. I still
    don't really get how to do this. the specifications say the program
    should take in the vector values as such:
    Size of the vector: 3
    Vector value: 2 2 1
    size of the vector is the 'n' input that i would pass in to
    read_vector()
    so i'm thinking of something like this...
    Code:
    void read_vector(int *v, int n)
    { 
       int i = 0, 
       value = 0; 
       printf("Vector value: "); 
       while (getchar() != '\n')
       { 
          scanf("%d", &value); v[i] = value; 
       }
    }

    but for some reason i still get an error... what's interesting is that it won't even get to the printf statement at the beggining....maybe its something from the previous function where the porblem lies???


    this one:
    Code:
    void addition(void) 
    {
    	int n1, n2;
    	
    	printf(" Size of the first vector: "); // vector 1
    	scanf("%d", n1);
    	int vector_1[n1];
    	read_vector(vector_1, n1);
    	
    	printf(" Size of the second vector: "); // vector 2
    	scanf("%d", n2);
    	int vector_2[n2];
    	read_vector(vector_2, n2);
    	
    	int nres = (n1 >= n2 ? n1: n2);
    	int result[nres];
    	
    	if ( vector_addition(vector_1, n1, vector_2, n2, result) == true)
    		print_vector(result, nres);
    	else printf("Error: vectors must be equal in size. Addition undefined.");
    	
    }

    i don't know what the hell is going on really.... haha.. I'm learning sorry.

    this is what I get in the prompt:

    ~/Desktop/P5 $./a.out

    Code:
    main menu:
    1. Vector..Addition 
    2. Scalar..Multiplication 
    3. Dot..Product 
    4. Outer..Product 
    5. Matrix..Addition 
    6. Matrix..Multiplication 
    7. Quit
    Enter your selection[1..7]:	1
     Size of the first vector: 3
    Bus error
    Last edited by fcommisso; 11-02-2009 at 09:16 AM.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    may be something simpler would work?

    Code:
    void read_vector(int *v, int n)
    {
    	int vec[n];
    	printf("vector value");
    	
    	for ( i = 0; i < n; ++i)
    	{
    		scanf("%d", vec[i]);
    		v[i] = vec[i];
    	}
    	
    	
    }

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    attached is a picture of my screen after the debugger highlighted (in blue) the line in my code where the program crashed. soo....it's the call of the function that that's messy... can't think of it... pointers is what keeps popping to my head since we are on "Pointer Arrays" chapter....

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    When you use scanf, you have to pass the address of a variable of where to store the value, you have:
    Code:
    scanf("%d", n1);
    ...
    scanf("%d", n2);
    But you need to have:
    Code:
    scanf("%d", &n1);
    ...
    scanf("%d", &n2);
    Also, as the link above describes, you need to "empty" the buffer after you enter the number, otherwise youll have characters left over in the input (keyboard) buffer, such as newline characters. So do something like:
    Code:
    scanf("%d", &n1);
    while (getchar() != '\n');
    Anytime youre reading a number.

    And finally, for the function that reads in the values of the vector, I would change it to read in one big string, with each number separated by a space. After that, you parse the numbers out of this string and store it in the array. Heres an example, see links: Cprogramming.com FAQ > Separate a string into tokens? (C) and atoi - C++ Reference
    Code:
    void read_vector(int *v, int n)
    {
            char buf[100]; //arbitrarily large size
            scanf("%99s", buf); // read in length of buf -1 (99) characters, with indices 0-98, and scanf will put '\0' at index 99 (max)
    
            // here you split the string into "tokens", each delimited by a space
            // in the while loop, you convert each "token" (in your case, it should be a string representation of a number) into an integer,
            // use function 'atoi' to convert the string to a number, and store it at 'v[i++]'
            // make sure the while loop does not execute more than 'i' times, otherwise it will be buffer overflow
    }
    Last edited by nadroj; 11-02-2009 at 10:16 AM.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    wow..nice.

    this is far more complicated than my professor would ever assign...but it's cool that I learn this.

    so you're saying I hsould include
    Code:
     while (getchar() != '\n');
    after ever scanf in my entire program to clear the buffer?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    I can't believe I missed all the & in every scanf call...i should have known better...I feel dumb

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    so you're saying I hsould include ...
    I think you only really need to clear the buffer after your getting a number. If they typed in "5x", "5" would be stored in the number, but "x\n" would be left in the buffer. So the next scanf you would do, would read from the buffer which is "x\n", so it basically skips the next user input (as it is already "typed").

    this is far more complicated than my professor would ever assign
    Well, there are much simpler ways to do it, however doing something outlined above would be "safer", which is why theres more effort in making it that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  3. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  4. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 08:33 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM