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.
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.
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);
}
}
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.
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>
usingnamespace 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;
}
elseif (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);
}
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.
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.
/*/* 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);
}
}
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".