Thread: Two easy vector questions.

  1. #1
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34

    Question Two easy vector questions.

    Hello everyone. My questions are the following:

    1) In a program, different values to a vector are given(e.g.: vector[1]=3; vector[2]=5; ...)

    Declaration: int vector[5]={1,5,6,7,2};
    And at the time of saving the info into a text file it would be something like this:
    Code:
    fprintf(fp,"%d %d %d %d", vector[1], vector[2], vector[3], vector[4])
    and the fscanf:
    Code:
    fscanf(fp,"%d %d %d %d", &vector[1], &vector[2], &vector[3], &vector[4])
    Imagine that it is a longer vector, then it would be a mess.

    Can there be written something like this?:
    Code:
    fprintf(fp,"%d %d %d %d", vector[i])
    
    fscanf(fp,"%d %d %d %d", &vector[i])
    2) I've heard around that there is a command or statement used to find values in a vector. Maybe:
    Code:
    while(5 in vector){printf("There is a 5 in the vector.");}
    But couldn't figure out how this is. Any help would be apreciated, if something isn't clear tell me, thanks.
    Last edited by Pecado; 10-17-2010 at 12:09 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    What is your declaration of vector?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Quote Originally Posted by Salem View Post
    What is your declaration of vector?
    I've added it to main post.
    Last edited by Pecado; 10-17-2010 at 12:09 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Arrays in C start at 0. In your code you are trying to write/read 1 past the end of your array.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    One more thing, use the code tags ONLY for code, not your entire post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Quote Originally Posted by Salem View Post
    One more thing, use the code tags ONLY for code, not your entire post.
    Took them off.

    Quote Originally Posted by jimblumberg View Post
    Arrays in C start at 0. In your code you are trying to write/read 1 past the end of your array.
    Well, yes, the declaration should be a vector[5] you are right, but anyways the question doesn't have to do with that... I've put the declaration in the main post. Thanks for the reminder anyways.
    Last edited by Pecado; 10-17-2010 at 12:08 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by Pecado View Post
    Can there be written something like this?:
    Code:
    fprintf(fp,"%d %d %d %d", vector[i])
    
    fscanf(fp,"%d %d %d %d", &vector[i])
    No. You can write a function to do this, though.
    2) I've heard around that there is a command or statement used to find values in a vector. Maybe:
    Code:
    while(5 in vector){printf("There is a 5 in the vector.");}
    There is no such thing. Again, a function can easily be written that does this.

    You can search for a single byte in an array, so for the special case of an array of character type, there's strchr() or memchr() (depending on whether it's a string or not).

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Pecado View Post
    Declaration: int vector[5]={1,5,6,7,2};
    And at the time of saving the info into a text file it would be something like this:
    Code:
    fprintf(fp,"%d %d %d %d", vector[1], vector[2], vector[3], vector[4])
    Yep, that would write "5 6 7 2" to your file. (You don't get 1 because vector[0] is 1 and vector[1] is 5.

    Quote Originally Posted by Pecado View Post
    and the fscanf:
    Code:
    fscanf(fp,"%d %d %d %d", &vector[1], &vector[2], &vector[3], &vector[4])
    That would be reading four integers from the file and storing them in vector[1] to vector[4] (Although what happens to vector[0]?)

    Quote Originally Posted by Pecado View Post
    Can there be written something like this?:
    Code:
    fprintf(fp,"%d %d %d %d", vector[i])
    
    fscanf(fp,"%d %d %d %d", &vector[i])
    I would go with something like
    Code:
    	for(int i = 0; i < VECTOR_SIZE; ++i)
    		fprint(fp, "%d ", vector[i]);
    
    	for(int i = 0; i < VECTOR_SIZE; ++i)
    		fscanf(fp, "%d ", &vector[i]); // Or if you want to be fancy: fscanf(fp, "%d ", vector+i);
    Quote Originally Posted by Pecado View Post
    2) I've heard around that there is a command or statement used to find values in a vector. Maybe:
    Code:
    while(5 in vector){printf("There is a 5 in the vector.");}
    No such command or language construct. I don't really know what you want, but you could have the following:

    Code:
    int find(int* vector, int size)
    {
    	for(int i = 0; i < size; ++i)
    	{
    		if(vector[i] == value) // Where value is what we are looking for
    		{
    			printf("The value %d exists in vector. First occurrence at vector[%d]\n", value, i);
    			return 1;
    		}
    	}
    	return 0;
    }

  9. #9
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Thank you "cas" and QuadraticFighte".

    Regarding the first, I've decided to do it the old fashioned way as I have many vectors and would rather not have so many for's.

    And for the second one, I don't know why I didn't think of a for construction. But thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM

Tags for this Thread