![]() |
|
#1
|
|||
|
|||
VectorsI have an hw assignment. Could someone help me about it.
Introduction The purpose of this lab is to familiarize you with accessing and manipulating vectors as well as using vectors as function arguments. Synopsis You will a program that first loads a vector with 5,000,000 (5 million) random integers and secondly adopts a function to search for a value in the vector, returning the number of occurrences of the search term found. Instructions: 1. Inside your main function (don’t forget the necessary includes!) create a vector object that will hold integers. You needn’t use the constructor that pre‐allocates memory, but if you do, pre‐allocate space for all 5x106 elements. 2. Use a loop to fill the vector with 5x106 random numbers. Make sure you use the push_back() method to store the values in the vector. 3. Write a function called findInt with the following prototype: a. int findInt(vector<int>& list, int term); b. Implement this function. It needs to loop through each element of the vector, count how many times term is found within the vector, and return that number back to the function caller. 4. In a do‐while loop, ask the user for the search term and receive it. 5. Call the findInt function with the search term and output the value the function returns – this is the number of occurrences of term in your vector. 6. Repeat this process until the user enters ‐1 as the search term, use this in the do‐while Boolean expression to exit the loop. Question: Why do you think the function used the vector through pass‐by‐reference vs. pass‐by‐value? Thanks; |
|||
|
#2
|
|||
|
|||
Re: VectorsQuote:
What kind of help would you like? I mean, do you have a textbook or other course materials or on-line reference or tutorial or what? In order to have an assignment about vectors, you must have covered something about vectors, right? In order to have an assignment that requires you to implement a function, you must have covered something about functions, right? Do one thing at a time. See Footnote. 1. Create a main function. 2. Inside the main function, create a vector object that will hold ints. 3. Compile that much. Obviously it won't do anything useful yet, but my advice is to compile each little bit before going to the next step. My experience shows that, by proceeding incrementally, I am less likely to type a bunch of invalid stuff that will have to be thrown away when I start trying to get it to work. 4. The next step might be to write a loop that generates a small number of random integers (maybe five or six) and puts them into the vector. Use the standard library function rand(). 5. Write a loop that prints out all of the elements of the vector. Your assignment is not to print them out, but this might get you used to putting stuff into vectors and inspecting the vector's contents. 6. Compile that much. Execute the program. You should see some numbers being printed. 7. etc. Regards, Dave Footnote: Quote:
|
|
#3
|
|||
|
|||
Re: VectorsQuote:
CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: Vectorsso far i have
CPP / C++ / C Code:
Now, I need to write code for to findint... any advice? |
|
#5
|
|||
|
|||
Re: VectorsQuote:
Code:
Regards, Dave |
|
#6
|
|||
|
|||
Re: VectorsThanks, Dave.
I think I'm done with it. Could you check it for me if I mail it to you? |
|
#7
|
|||
|
|||
Re: VectorsQuote:
You are welcome. You have been brave enough to let everyone see your efforts so far, and I understand if you don't want to put your finished project up for grabs, but it's a public forum. We all benefit by keeping things public. Regards, Dave |
|
#8
|
|||
|
|||
Re: Vectorshi dave,
let me introduce myself, i am swaroop, joined now in the forum, from a long time i was reading your replies, i am very impressed with your answers so far, nice to meet you in the forum, cheers swar |
|
#9
|
||||
|
||||
Re: VectorsQuote:
Something like: CPP / C++ / C Code:
Output: Code:
A: ...passing by value creates a copy of of the object passed. A copy of 5 million integers would be considered "expensive" and could create a stack allocation exception. I then used 0x4C4B40 as the range for the RNG and got this output: Code:
...which is a lot more fun. What would be even more fun would to ensure that the randoms were unique. That would make it a bit more of a guessing game to find an integer that matches the input. MxB |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Vector Issues | Peter_APIIT | C++ Forum | 16 | 21-May-2007 08:40 |
| Generic searching through vectors | zdenek | C++ Forum | 1 | 10-Oct-2006 09:44 |
| partition a vector of vectors | acosgaya | C++ Forum | 0 | 06-Oct-2006 10:27 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 07:17 |
| vectors of references | mirizar | C++ Forum | 1 | 12-Apr-2005 03:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The