Totally screwed on comp science course!

Pages: 123
Hi everyone, im taking an online c++ course, and i have 5 more programs left to write that are due tomorrow by midnight! I do not get how to write these and am asking for your help. If i dont get this done, i will fail the course, resulting in a waste of money for registering in it.

If anyone could assist me in writing some of these programs, please post below.

I am begging you; the situation is dire!


Thanks,

Disgruntled c++ student
nobody's going to help you if you don't even make an attempt at solving the problems. Write some code, then post it here if it doesn't do what you expect.
i did try, except it is beyond my ability to understand the concepts, which is why im posting here
post what you tried, and what you expected to happen, and you'll get some responses.
What's the first concept you don't understand?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Ok first program is to find prime numbers between 50 and 100. Here is the code, i got, which only works to an extent.


/*
* File name: prime.cpp
* This program displays all prime numbers between 50 and 100.
*/

#include <stdio.h>
#include <math.h>
#include <genlib.h>

int main()
{
	int v, g, prime;
	printf ("The prime numbers between 50 and 100 are:\n");
	for (g=1; g<=100;g++)
	{
		prime=1;
		for (v=2; v<g; v++)
		{
	if (g%v==0)
	prime=0;
		}
		if (prime==1)
			printf ("%i\n",g+2);
	}
}
Last edited on
That looks suspiciously like C, not C++. w/e!

The simplest (albeit not very efficient) way is to just check if any number below a number of interest divides that number. If it doesn't, numberofinterest % numberbelow == 0 will return false. You'll need a for loop to change the number of interest, and another nested for loop to increment the numberbelow. Remember to have an array to store some sort of indication that a certain number is prime.

Nevermind.

-Albatross

Last edited on
nah i was wrong it output 9 as a prime when I tested it, sorry
Last edited on
Why are you adding 2 to g when you print it out? If not for that, it should work perfectly. It does, actually...

-Albatross
Last edited on
You add two to g for some reason, get rid of it that should fix it

EDIT: too slow
Last edited on
edit: how about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool prime(int check){
	int temp = 1;
	while (temp < check){
		temp += 1; //2
		if (check % temp == 0 && temp < check)
			return false;
		temp += 1; //3
		if (check % temp == 0 && temp < check)
			return false;
		temp += 2; //5
		if (check % temp == 0 && temp < check)
			return false;
		temp += 2; //7
		if (check % temp == 0 && temp < check)
			return false;
		temp += 2; //9
		if (check % temp == 0 && temp < check)
			return false;
		temp += 2; //11
		if (check % temp == 0 && temp < check)
			return false;
	}
	return true;
}


for 10000 numbers it tested at 17220 microseconds
the other method took 25096 microseconds
Last edited on
1
2
3
4
/*
* File name: prime.cpp
* This program displays all prime numbers between 50 and 100.
*/

That means the loop for the outer variable (g) should go from 50 to 100, not 1 to 100.

One down, four to go. That wasn't so bad was it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Yes thank you so much everyone for helping me out on the previous program! 

Now the next one is to write a program that shows the Greatest Common Divisor (GCD) the rough way.

Here is my code:

/*
* File name: GCD1.cpp
* This program does GCD the brute force way.
*/

#include <stdio.h>
#include <stdlib.h>
#include "genlib.h"
#include "simpio.h"
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
	int n1, n2, n3, n4, rem, rem1;
	printf ("Enter x: ");
	n3= GetInteger();
	printf ("Enter y: ");
	n4= GetInteger();
	if (n3<n4)
	{
		n1=n4;
		n2=n3;
	}
	else if (n3>n4)
	{
		n1=n3;
		n2=n4;
	}
	rem= n1%n2;
	rem1=0;
	if (rem==0)
		printf ("The GCD is %d\n", n2);
	while (rem>0)
	{
		n2=n2-1;
		rem=n1%n2;
		rem1= rem1+1;
	}
	if (rem1>0)
		printf ("The GCD is %d\n", rem1);
}
Last edited on
I'd use a for loop rather than a while loop when getting the GCD.
Last edited on
Please edit your posts and put the source within code tags. It will be much easier to read.
Use this after the ifs:
1
2
3
4
5
6
7
8
9
    while((rem=n1%n2) != 0) //Test the remainder
    {
        n1 = n2; //Now n1 becomes n2
        n2 = rem; //Now n2 becomes the remainder of n1 and n2.
        //Notice that the remainder of two numbers is smaller then both,
        //so n1 > n2 still after this statement.
    }

    printf ("The GCD is %d\n", n2);


Here are the errors in your original:
1
2
3
4
5
6
7
8
9
10
11
12
rem= n1%n2;
rem1=0;
if (rem==0)
printf ("The GCD is %d\n", n2);
while (rem>0)
{
n2=n2-1; //you decrement n2, loosing information of what its divisors originally were
rem=n1%n2; //you take the remainder of all numbers starting from n2 down to 0. How does this find GCD?
rem1= rem1+1; //you increment every time by 1 starting from 0. What for?
}
if (rem1>0)
printf ("The GCD is %d\n", rem1);


You were trying to copy the logic of the Prime number tests, but this is not the same ordeal. What you had to use (and what I used) is the Euclid's algorithm. You can google it some time in the future to see why it works.
Last edited on
Thank you very much!
Ok the next program is to write a program that reads a list of strings until the word "end" appears and returns the longest string that was included in the input list.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*/* File name: longStr.cpp
* This program reads a list of strings until the word "end" appears
* and returns the longest string that was included in the input list
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

int main()
{
	string string, resultStr, curStr;
	resultStr= "";
	printf ("Signal end of output with the world end\n");
	printf ("Enter next string");
	curStr= GetLine();
	while (!StringEqual (resultStr, curStr)))
	{
		resultStr=concat(resultStr, curStr);
		printf ("Enter next string: ");
		curStr= GetLine();
	}
		printf ("The concareation of all the input strings is %s", resultStr);
	}
}
Last edited on
StringEqual()? concat()? Why not just use == and +? And why are you concatenating them anyway? I didn't see anything about that in the problem description. You need to check the size() of each string as you read them in and store it somewhere if it is the longer so far until you get to "end".

And use [code][/code] tags, please.
can you give me an example using the == and + in the specified areas?
Pages: 123