473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

vectors and string copy

I am doing private studies on c++ and have no tutors. Can someone help me with the following. I will write exams end of the year in c++

4. how can one write a c++ programme which does the following

a create a vector of size 1000 and have it store the values 0.1, 1.1, 2.1, 3.1,....999.1

b trucate the vector to a size of 5

c print out how many values the vector is storing

d empty out the vector so that it stores no values


5. rewrite the following code correcting all problems found

void calculatevalues (const int &a, float *b, int &c)

{
d = 20.4;

if (*a =0)

*a += 1
c = a +d;

else

b= c/d ;

c= a- b +d;

}

Hint if a =4, b =0.0 and c=5 when this function is called, their values when this fucntion returns should be a=4, b=0.25 and c= 24.15

6 a The string class in the standard c++ library is a template for any type character of character representation.

write a function called copy which takes two parameters of the string type. The first parameter is the source string ( or the string to be copied) and the second parameter is the target string ( or the string that recieves the copy). This function need not return anything.

One is not allowed to use the fuction strcpy (.....)!
Feb 8 '07 #1
6 1439
Ganon11
3,652 Expert 2GB
What have you done on your own to solve these problems?
Feb 8 '07 #2
What have you done on your own to solve these problems?
This is what i have done:

Expand|Select|Wrap|Line Numbers
  1. a) .# include < iostream>
  2.    #include < vector>
  3. int main ()
  4.  
  5. {
  6.      vector <int> ( 1000)
  7.  
  8.  
  9.      }
  10.  
  11. b) truncate vector to size of 5
  12.  
  13. # include < iostream>
  14. # include  <vector>
  15.  
  16. int main ()
  17.  
  18. {
  19.   vector <int> jc (1000)
  20.  
  21.      jc.resize (5)
  22.  
  23. cout << jc.size ();
  24.    }
  25.  
  26. c) print out how many values in vector file
  27.  
  28. # include < iostream>
  29. # include  <vector>
  30.  
  31. int main ()
  32.  
  33. {
  34.   vector <int> (1000)
  35.     cout << " size () returns " < size () << endl;
  36.  
  37.   }
  38.  
  39. d Print so that it stores no vector
  40.  
  41.  
  42. # include < iostream>
  43. # include  <vector>
  44.  
  45. int main ()
  46. {
  47.  
  48.  vector <int>  jc (1000)
  49. jc.clear ()
  50.  
  51. cout<< jc.size (); prints 0
  52.  
  53. 6.
  54. {
  55.  if (a==0)
  56.  
  57. else 
  58.   b= c/d
  59. }
Feb 8 '07 #3
Expand|Select|Wrap|Line Numbers
  1. a) .# include < iostream>
  2.    #include < vector>
  3. int main ()
  4.  
  5. {
  6.      vector <int> ( 1000)
  7.  
  8.  
  9.      }
  10.  
  11. b) truncate vector to size of 5
  12.  
  13. # include < iostream>
  14. # include  <vector>
  15.  
  16. int main ()
  17.  
  18. {
  19.   vector <int> jc (1000)
  20.  
  21.      jc.resize (5)
  22.  
  23. cout << jc.size ();
  24.    }
  25.  
  26. c) print out how many values in vector file
  27.  
  28. # include < iostream>
  29. # include  <vector>
  30.  
  31. int main ()
  32.  
  33. {
  34.   vector <int> (1000)
  35.     cout << " size () returns " < size () << endl;
  36.  
  37.   }
  38.  
  39. d Print so that it stores no vector
  40.  
  41.  
  42. # include < iostream>
  43. # include  <vector>
  44.  
  45. int main ()
  46. {
  47.  
  48.  vector <int>  jc (1000)
  49. jc.clear ()
  50.  
  51. cout<< jc.size (); prints 0
Feb 8 '07 #4
What have you done on your own to solve these problems?
I am doing private studies on c++ and have no tutors. Can someone help me with the following. I will write exams end of the year in c++. see the answers that i have tried to write right at the end

4. how can one write a c++ programme which does the following

a create a vector of size 1000 and have it store the values 0.1, 1.1, 2.1, 3.1,....999.1

b trucate the vector to a size of 5

c print out how many values the vector is storing

d empty out the vector so that it stores no values


5. rewrite the following code correcting all problems found

void calculatevalues (const int &a, float *b, int &c)

{
d = 20.4;

if (*a =0)

*a += 1
c = a +d;

else

b= c/d ;

c= a- b +d;

}

Hint if a =4, b =0.0 and c=5 when this function is called, their values when this fucntion returns should be a=4, b=0.25 and c= 24.15

6 a The string class in the standard c++ library is a template for any type character of character representation.

write a function called copy which takes two parameters of the string type. The first parameter is the source string ( or the string to be copied) and the second parameter is the target string ( or the string that recieves the copy). This function need not return anything.

One is not allowed to use the fuction strcpy (.....)!


answers


5 i) .# include < iostream>
#include < vector>
int main ()
{
Vector <int> jc;
Jc. Resize (1000);
Jc[ 1000-1] = 999.1;
}

ii) //Truncate vector file to size of 5
# include <iostream>
# include < vector>
Int main ()
{
Vector <int> jc (1000);
Jc.resize (5);
Cout << jc.size ();
}

iii) //print how many vector is storing
# include <iostream>
# include < vector>
Int main ()
{
Vector <int> (1000);
Cout << “ size () returns “ < size () << endl;
}
iv) // Empty the vector file
# include <iostream>
# include < vector>
Int main
{
Vector <int> jc (1000);
Jc.clear ();
Cout << jc.size (); // prints 0
}

7. Instead of using the

#include <list>
#include <vector>
#include <iostream>

int main()
Feb 8 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
I have merged two of your threads together that have the same subject matter. This may lead to some duplication of posts. If you would like some of the posts deleted reply and tell me which numbers to delete.

Please don't double post.

ADMIN
Feb 9 '07 #6
I have merged two of your threads together that have the same subject matter. This may lead to some duplication of posts. If you would like some of the posts deleted reply and tell me which numbers to delete.

Please don't double post.

ADMIN
can remove the second one
regards
Feb 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
8
by: slurper | last post by:
if i have a vector and assign another vector to a vector variable like this: vector<int> c, k; c.push_back(1), c.push_back(2); k.push_back(3), k.push_back(4); c = k; does this work?? if...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
10
by: jois.de.vivre | last post by:
Hi, I'm wondering exactly how the STL vector is making a copy of my object. For example: #include <iostream> #include <cstdlib> #include <vector> using namespace std; class test {
10
by: edward_nig | last post by:
Hi all, Please I tried to copy from a text file line by line and to paste the text in reverse order in another text file also line by line-Making the first line the last and the last line the...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
5
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.