I need to list the first prime numbers starting from 2 and going up until 100, and I'm getting this output (lots of them are composite numbers, like 4 and 25):
2
3
4
5
7
9
11
13
17
19
23
25
29
31
37
41
43
47
49
53
59
61
67
71
73
79
83
89
97
And this is the code I ran:
Code:
#include <iostream>
#include <cmath>
using namespace std;
bool isDivisible(int number, int divisor);
bool isPrime(int number);
void keep_window_open();
int main()
{
for (int i = 2; i < 100; i++)
{
if (isPrime(i))
{
cout << i << "\n";
}
}
keep_window_open();
}
bool isPrime(int number)
{
for (int i = 2; i < sqrt((double)number); i++)
{
if (isDivisible(number, i))
{
return false;
}
}
return true;
}
bool isDivisible(int number, int divisor)
{
if (number % divisor != 0)
{
return false;
}
return true;
}
void keep_window_open()
{
cin.clear();
char ch;
cout << "Please enter a character to exit: ";
cin >> ch;
cin.ignore();
return;
}
And it's giving me the error "i does not define a type" whenever I try to do in my for-loop header, plus there's the problem of it saying there should be a closing brace before numeric constant or a comma or semicolon or something before it and stuff when I try to initialize a vector of integers 2 through 100 in curly-braces separated by commas. I've got the flag for std=c++14 turned on; maybe I should change it to c++1y or c++11, although it's never bothered by like this before now. Something strange is going on.
Anyway, the one for checking whether two numbers are almost equal or not is doing fine now, I think. I just need to fix the one where I have to take in one number as input each time through the loop and check which is one is the smallest or largest so far, and the incorporate the distance measurements into it.
The program where I have to take in one number as input each time through the loop and check for the largest and smallest numbers is giving me an output like this:
10 0 100 -1|
0: the smallest so far
0: the smallest so far
100: the largest so far
100: the largest so far
0: the smallest so far
100: the largest so far
100: the largest so far
-1: the smallest so far
-1: the smallest so far
-1: the smallest so far
.....
The rest after that is as it should be, but why is it repeating the numbers more than once like that? How do I fix it?
This is the code.
Code:
#include <iostream>
#include <vector>
using namespace std;
void keep_window_open();
int main()
{
double larger;
double smaller;
double number = 0;
vector<double> user_input;
cout << "Keep entering numbers; to exit the program, just enter something that isn't a number.\n";
while (cin >> number)
{
user_input.push_back(number);
for (unsigned i = 0; i < user_input.size(); i++)
{
for (unsigned j = 0; j < i; j++)
{
if (user_input[i] < user_input[j])
{
smaller = user_input[i];
cout << smaller << ": the smallest so far\n";
}
else if (user_input[i] > user_input[j])
{
larger = user_input[i];
cout << larger << ": the largest so far\n";
}
}
}
}
int number_of_elements = user_input.size();
cout << "The smallest value is: " << smaller << "\n";
cout << "The largest value is: " << larger << "\n";
cout << "The number of items is: " << number_of_elements << "\n";
keep_window_open();
cin.ignore();
}
void keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit: ";
char ch;
cin >> ch;
cin.ignore();
return;
}
I'll try to do the pseudo code for the distance incorporation in a bit here.
Edit: Never mind; that code is still producing weird output.