Output, input/output, and averages

Needs to output first 100 prime numbers.

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
#include <iostream>

using namespace 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.

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
#include <iostream> 

using namespace 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.

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
#include <iostream>
using namespace std;

int main()
{
	int z = 0;
	int size = z;
	double *x = new double [z];
	int num = 0;
	double sum = 0;

	for ( size = 0; size < num; z++)
	{
		cout << "Please enter a number: ";
		cin >> x[z];
		sum += x[z];
		num ++;

		if(num >= size)
		{
			double *temp = new double [size + 2];
			*x = *temp;
			delete x;
			x = temp;
		}
		if( x[num] = -999)
			break;
	}

	double average = sum / num;


	int count = 0; 

		for ( int i = 0; i < num; i++)
		{
			if (x[i] >  average) count ++;
		}

	cout << "Count = " << count << endl;
	return 0;
}


Please help me find out what I'm doing wrong with these. Thank you.

Last edited on
Needs to output first 100 prime numbers.


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>


int main()
{
	unsigned int prime_numbers[100];
	unsigned char pn_end = 0;
	unsigned int curr_number = 2;
	while(pn_end < 100)
	{
		unsigned int i = 0;
		while(i < pn_end && curr_number % prime_numbers[i])
			i++;
		if (i == pn_end)
			prime_numbers[pn_end++] = curr_number;
		curr_number++;
	}

	for(unsigned int i = 0; i < pn_end; i++)
		std::cout << i+1 << ": " << prime_numbers[i] << std::endl;

	return 0; 
}



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!
Last edited on
It works! Thanks so much! Do you know how I could fix the other two codes?
Last edited on
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.


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
#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? ";
	unsigned int count;
	std::cin >> count;

	/* Read them */
	for(unsigned int i = 0; i < count; i++)
	{
		std::cout << "Insert a number: ";
		unsigned int 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.


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
#include <iostream>
#include <set>
#include <iterator>

template<class T> class MeanValuer {
public:
	MeanValuer() : mean_val(), entries(0) {}
	void operator()(const T& val) { mean_val =
			(mean_val*entries+val)/(entries+1); entries++; }
	T get_mean_val() const { return mean_val; }
private:
	T mean_val;
	unsigned int 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? ";
	unsigned int count;
	std::cin >> count;

	/* Read them */
	for(unsigned int i = 0; i < count; i++)
	{
		std::cout << "Insert a number: ";
		unsigned int 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;
}

Topic archived. No new replies allowed.