GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Mar-2009, 13:57
ultrAslan ultrAslan is online now
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 16
ultrAslan has a little shameless behaviour in the past

Vectors


I 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  
Old 08-Mar-2009, 16:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Vectors


Quote:
Originally Posted by ultrAslan
...Could someone help ...

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:
Originally Posted by Agatha Christie
"We can face our problem.
We can arrange such facts as we have
with order and method."

Hercule Poirot
--- in Murder on the Orient Express
  #3  
Old 08-Mar-2009, 18:38
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Vectors


Quote:
Originally Posted by ultrAslan
Question: Why do you think the function used the vector through pass‐by‐reference
vs. pass‐by‐value?
If you understand the following:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

void f(int);
void g(int*);
void h(int&);

int main() {
    int i = 0;

    cout << "i = " << i << endl;
    f(i);
    cout << "i = " << i << endl;
    g(&i);
    cout << "i = " << i << endl;
    h(i);
    cout << "i = " << i << endl;

    return 0;
}

void f(int j) {
    j = 1;
}

void g(int *p) {
    *p = 2;
}

void h(int &j) {
    j = 3;
}
...then you should see that the fundmental concepts illustrated here answer your question.
  #4  
Old 08-Mar-2009, 19:29
ultrAslan ultrAslan is online now
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 16
ultrAslan has a little shameless behaviour in the past

Re: Vectors


so far i have

CPP / C++ / C Code:
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;

int findInt(vector<int>& list, int term);

int main()
{
	int input, n = 15;
	srand(time(NULL));
	vector<int> the_vector;
	
	for (int i=0; i<n; i++)
	{
		the_vector.push_back(rand()%10);
		cout << the_vector[i] << "\t";
	}
	
	do {
		cout << "\nEnter a value to examine or e to exit\n";
		cin >> input;
		
	} while ((toupper(input) == 'E'));
	
	return 0;
	 
}
	

Now, I need to write code for to findint... any advice?
  #5  
Old 09-Mar-2009, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Vectors


Quote:
Originally Posted by ultrAslan
...code for to findint... any advice?


Code:
1. Declare an int named retvalue. Initialize it to zero. 2. Make a loop that goes through all of the members of the vector. You can use an int, i, as the loop counter and use the vector class size() member function to determine the loop upper limit. Each time through the loop, if the value of list[i] is equal to the value of term, increment retavalue. 3. Return retvalue.

Regards,

Dave
  #6  
Old 09-Mar-2009, 13:17
ultrAslan ultrAslan is online now
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 16
ultrAslan has a little shameless behaviour in the past

Re: Vectors


Thanks, Dave.

I think I'm done with it. Could you check it for me if I mail it to you?
  #7  
Old 09-Mar-2009, 15:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Vectors


Quote:
Originally Posted by ultrAslan
Thanks, Dave...

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  
Old 15-Mar-2009, 04:33
malavath malavath is offline
New Member
 
Join Date: Mar 2009
Posts: 1
malavath is on a distinguished road

Re: Vectors


hi 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  
Old 15-Mar-2009, 09:22
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Vectors


Quote:
Originally Posted by ultrAslan
I 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;

Something like:

CPP / C++ / C Code:
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

typedef std::vector<int> intVector;

int findInt(intVector& v, int term)
{
    int retval = 0;
    intVector::iterator it;
    for(it = v.begin(); it != v.end(); it++)
    {
        if(*it == term)
        {
            ++retval;
        }
    }
    return retval;
}

int main()
{
    intVector v;
    srand((unsigned)time(0));
    for(size_t count = 0; count < 0x4C4B40; count++)
    {
        v.push_back((1 + (int)(1000 * (rand()/(RAND_MAX + 1.0)))));
    }
    int value;
    do
    {
        cout << "Enter a positive integer value (-1 to quit): ";
        cin >> value;
        if(value != -1)
        {
            cout << "The integer you entered was found " << findInt(v, value) << endl;
        }
    } while(value != -1);

    return 0;
}


Output:

Code:
Enter a positive integer value (-1 to quit): 34 The integer you entered was found 5003 Enter a positive integer value (-1 to quit): 43 The integer you entered was found 5024 Enter a positive integer value (-1 to quit): 1 The integer you entered was found 5030 Enter a positive integer value (-1 to quit): 1000 The integer you entered was found 5048 Enter a positive integer value (-1 to quit): 765 The integer you entered was found 4916 Enter a positive integer value (-1 to quit): 2 The integer you entered was found 4977 Enter a positive integer value (-1 to quit): 8 The integer you entered was found 4993 Enter a positive integer value (-1 to quit): 3928 The integer you entered was found 0 Enter a positive integer value (-1 to quit): 800 The integer you entered was found 5079 Enter a positive integer value (-1 to quit): -1

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:
Enter a positive integer value (-1 to quit): 32 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 09 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 11 The integer you entered was found 0 Enter a positive integer value (-1 to quit): 29 The integer you entered was found 3 Enter a positive integer value (-1 to quit): 129183 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 838290 The integer you entered was found 0 Enter a positive integer value (-1 to quit): 33211 The integer you entered was found 0 Enter a positive integer value (-1 to quit): 339281 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 1234567 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 123456 The integer you entered was found 2 Enter a positive integer value (-1 to quit): 8473473 The integer you entered was found 0 Enter a positive integer value (-1 to quit): 5000000 The integer you entered was found 1 Enter a positive integer value (-1 to quit): 1111111 The integer you entered was found 4 Enter a positive integer value (-1 to quit): -1

...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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 02:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.