For my intro to c++ class I have to create a complete program from broken algorithms
The questions are
(a) Print a random number between -1 and -9 to the output screen:
(b) Print (to the output screen) the sum of the square roots of the numbers 1, 2, 3 , 4, 5 and 6.
c) Ask the user to enter the word ”Hello”. Force the user to keep entering a new word until an input equal to ”Hello” is received.
(d) Print twelve random negative numbers.
(e) Print the largest integer whose square root is less than 1729.

So far I have

a)int r = rand() % 9 + 1;
cout << -r;

b)double sum = 0;
for (int s = 1; s <= 6; s++)
sum += sqrt(s);
cout << sum;

c)string input = "";
while (input != "Hello") {
cout << "Please enter the word ’Hello’: ";
cin >> input;
}

d)for (int n = 1; n <= 12; n++) {
int r = rand();
if (r > 0) r = -r;
cout << r;
}

e)int n = 1;
while (sqrt(n) < 1729) n++;
cout << n - 1;

How can I make a program that incorporates all these algorithms and that flows well. Thanks

Recommended Answers

All 7 Replies

A good start would be to put each of those code fragments into separate routines, and post the code here.

Please put the code in tags for easier reading, too. Thanks.

a)int r = rand() % 9 + 1;
cout << -r;
b)double sum = 0;
for (int s = 1; s <= 6; s++)
sum += sqrt(s);
cout << sum;
c)string input = "";
while (input != "Hello") {
cout << "Please enter the word ’Hello’: ";
cin >> input;
}
d)for (int n = 1; n <= 12; n++) {
int r = rand();
if (r > 0) r = -r;
cout << r;
}
e)int n = 1;
while (sqrt(n) < 1729) n++;
cout << n - 1;

I hope this helps, I just need to make a single program that Incorporates all the codes and that flows well. Thanks for the help.

Thanks for putting your code in blocks. What I was looking for, though, was for you to build compilable routines. Using your code, I'd look for something like this:

void randNbr()
{
	int r = rand() % 9 + 1;
	std::cout << -r;
}

Note that for this routine to work, you'll need to include a couple of header files. Can you figure out which ones?

See if you can do your other routines like the example I gave you above.

Once you have all your subordinate routines done, it should be easy to write the main routine and put it all together.

Unfortunately I am totally lost, and we are just learning about functions. Since I have many other problems like these I was hoping someone can do this for me and I will use it as a model to solve my other problems. Any help would be appreciated. Thanks.

If you're learning about functions, then now is the perfect time to start creating some, right? :)

You can put your code fragments all in one program by simply pasting them into the center of:

#include <iostream>
#include <stdlib.h>

int main (int argc, char *argv[])
{
   // program code goes here

   return 0;
}

As far as "flows well", I have no idea what you'd like it to do. That part is entirely up to you. What do -you- mean by "a single program ... that flows well"?

Unfortunately I am totally lost, and we are just learning about functions. Since I have many other problems like these I was hoping someone can do this for me and I will use it as a model to solve my other problems.

I gave you a valid example of a function; can you use it as a model to create your other functions? I'm happy to assist, but you need to do most of the work.

To use all those functions in one program you need to call them in main at different stages (or as you need). You can use conditional statement( if else,switch case) like :

#include <iostream>
void randnmbr();

int main()
{
int x;
cout<<"Enter 1 for printing a random number....";
cout<<"Enter 2 for something else....(so on)";
cin>>x;                //for switching
switch(x)
{
case 1: {...; break;}    // call a function
case 2: {...; break;} 
}
return 0; 
}

void randnmbr()  //definition of the function (refer book for working of a func)   
{
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.