#include <iostream>
usingnamespace std;
int main()
{
int prime = 2;
int count;
int number;
cout << "The first 100 prime numbers are: " << endl;
for (count = 1; count < 300; prime ++)
{
for (number = 2; number != prime; number ++)
{
int end;
int end2;
end = prime % number;
end2 = end * number;
if (number == end2)
{
cout << "(" << count++ << ") " << number << endl;
}
}
}
return 0;
}
Needs to to read in a series of numbers and display a count of how many numbers are larger than the last.
#include <iostream>
usingnamespace std;
int main()
{
cout << "How many numbers will you be inputing? ";
int howMany;
cin >> howMany;
double numbers[10000];
int counter = 0;
for (int num = 0; num < howMany; num++)
{
cout << "Enter a number: ";
cin >> numbers[num];
for (num = 0; num < howMany; num++)
{
if(num > num + 1)
{
counter ++;
}
}
}
cout << "There are " << counter << " numbers that are greater than the last." << endl;
return 0;
}
Needs to to read in a series of numbers and display how many are greater than their average.
I didn't manage to understand the code you wrote down. I stopped reading at for (count = 1; count < 300; prime ++). Next time try to avoid using a for statement in such a way (that is initializing, checking and incrementing different variables).
I come up with the following, maybe you can fix up your code accordingly? I wrote it down in a rush but it seems to work, I wouldn't be surprised if it brakes down, just use it as a principle reference.
BTW: I used an array instead of a more cplusplus-ish std::vector because I don't know whether you've already been introduced to it. If you do know the basics of std::vector go for it instead of using c-style arrays!
I'm not good at understanding other's code, anyways I have some of mine that hopefully can do the trick.
BTW: The following uses the STL, if you're not supposed to use it (maybe you're doing some kind of homework?) you can use the following as a reference skeleton, thus making your own implementation of the concepts exposed, using arrays instead of std::vectors, for statements instead of STL algorithms and so on.
Needs to to read in a series of numbers and display a count of how many numbers are larger than the last.
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
int main()
{
/* Asks the user how many numbers are we supposed to read */
std::vector<double> numbers;
std::cout << "Reads a certain amount of numbers and ""prints out how many numbers are greater than ""the last one.\n""How many numbers do you want me to read? ";
unsignedint count;
std::cin >> count;
/* Read them */
for(unsignedint i = 0; i < count; i++)
{
std::cout << "Insert a number: ";
unsignedint curr_num;
std::cin >> curr_num;
numbers.push_back(curr_num);
}
/* Calculates and outputs the amount of numbers which are greater than the last inserted */
std::vector<double>::difference_type greaters =
std::count_if(numbers.begin(), numbers.end(),
std::bind2nd(std::greater<double>(), numbers.back()));
std::cout << (greaters != 1 ? "There are " : "There is ") << greaters <<
(greaters != 1 ? " numbers which are " : " number which is ") <<
"greater than the last you inserted." << std::endl;
}
Needs to to read in a series of numbers and display how many are greater than their average.
#include <iostream>
#include <set>
#include <iterator>
template<class T> class MeanValuer {
public:
MeanValuer() : mean_val(), entries(0) {}
voidoperator()(const T& val) { mean_val =
(mean_val*entries+val)/(entries+1); entries++; }
T get_mean_val() const { return mean_val; }
private:
T mean_val;
unsignedint entries;
};
int main()
{
/* Asks the user how many numbers are we supposed to read */
std::set<double> numbers;
std::cout << "Reads a certain amount of numbers and ""prints out how many numbers are greater than""the average value of all the entered numbers...\n""How many numbers do you want me to read? ";
unsignedint count;
std::cin >> count;
/* Read them */
for(unsignedint i = 0; i < count; i++)
{
std::cout << "Insert a number: ";
unsignedint curr_num;
std::cin >> curr_num;
numbers.insert(curr_num);
}
/* Calculates and outputs the mean value */
double mean_val = std::for_each(numbers.begin(), numbers.end(),
MeanValuer<double>()).get_mean_val();
std::cout << "The mean value of the read numbers is " << mean_val << '.' << std::endl;
/* Calculates and outputs the amount of numbers which are greater than the mean value */
std::set<double>::difference_type greaters =
std::count_if(numbers.begin(), numbers.end(),
std::bind2nd(std::greater<double>(), mean_val));
std::cout << (greaters != 1 ? "There are " : "There is ") << greaters <<
(greaters != 1 ? " numbers which are " : " number which is ") <<
"greater than the the mean value." << std::endl;
}