Small help needed to complete an arrays assignment! Beginner programming.

Hello, thank you for checking on my question! So I am making a program declares an array of 100 doubles. I got it to start displaying the doubles in the correct format, but I can't figure out how to make them random. My code only displays one set of numbers every time I run it. Any and all help will be appreciated! Take care!
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
  #include "stdafx.h"
  #include <stdio.h>
  #include <stdlib.h>

  int main() {
	double randomnumbers[100] = { 0 };
	int i;
	double randomaverage = 0;
	double randomtotal = 0;
	for (i = 0; i < 100; i++)
	{


		randomnumbers[i] = (double)(rand() % 100 + 1);
		printf(" %6.2lf  ", randomnumbers[i]);

		if ((i + 1) % 10 == 0)
			
			printf("\n");

		randomtotal += randomnumbers[i];
	}
	randomaverage = randomtotal / 10;
	printf("%lf", randomaverage);
	system("pause");
}
Hello okancnplt,

You never seed the RNG, so it starts with the same numbers every time.

Try adding this before the for loop. srand(static_cast<size_t>(time(0)));. Do not forget to include "<time.h>" with your other headers.

Hope that helps,

Andy
Hey Andy! Thanks for your reply! So I messed around with it a bit and had to switch compilers, so now this is in C instead of c++. I tried to make improvements to this using your help, but now I am getting an error. (error: expected expression before 'size_t')

Here is the line of 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
27
28
29
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <time.h>

  int main() {
	double randomnumbers[100] = { 0 };
	int i;
	double randomaverage = 0;
	double randomtotal = 0;

	srand(static_cast(size_t)(time(0));
	for (i = 0; i < 100; i++)
	{


		randomnumbers[i] = (double)(rand() % 100 + 1);
		printf(" %6.2lf  ", randomnumbers[i]);

		if ((i + 1) % 10 == 0)

			printf("\n");

		randomtotal += randomnumbers[i];
	}
	randomaverage = randomtotal / 10;
	printf("%lf", randomaverage);
	system("pause");
}
Last edited on
Hello Baps,

Sorry about the wrong name in my last post. I am not sure how that got there.

The only problem I found is with line 12. It is not the same as what I showed you. The first set of () should be <>. Compare your line 12 to my line 12 and notice the difference.

Your program works in C and does the job. Here is what it might look like in C++:

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 <iomanip>
#include <ctime>

int main()
{
	double randomnumbers[100]{};  // <--- Equalivant to what you had.
	int i{};  // <--- Should ALWAYS initialize your variables.
	double randomaverage = 0;
	double randomtotal = 0;

	srand(static_cast<size_t>(time(0)));

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	for (i = 0; i < 100; i++)
	{
		randomnumbers[i] = (double)(rand() % 100 + 1);
		std::cout << std::setw(6) << randomnumbers[i] << "  ";

		if ((i + 1) % 10 == 0)

			std::cout << "\n";

		randomtotal += randomnumbers[i];
	}

	randomaverage = randomtotal / 10;

	std::cout << "\n The averge is: " << randomaverage << "\n\n" << std::endl;

	system("pause");
}


The big block of numbers might be a little different than what you C program dose I did not heck to match the outputs up, but it does give you an idea of what could be done.

Hope that helps,

Andy
Hey Andy,
That's ok re: the name haha. I wondered about that for a second though!
So I still am unable to open and run the programs. Since we started moving things around I haven't gotten to see an array table at all. What would be the equivalent to std::cout << std::fixed << std::showpoint << std::setprecision(2); for me?
Here is what I'm looking at now. I'm getting lots of errors regarding static_cast and size_t. Are these normal variables in c?

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 <stdio.h>
#include <stdlib.h>
#include <time.h>

  int main() {
	double randomnumbers[100] = { 0 };
	int i;
	double randomaverage = 0;
	double randomtotal = 0;

	srand(static_cast<size_t>(time(0));

	printf(" %6.2lf  ", randomnumbers[i]);

	for (i = 0; i < 100; i++)
	{


		randomnumbers[i] = (double)(rand() % 100 + 1);
		printf(" %6.2lf  ", randomnumbers[i]);

		if ((i + 1) % 10 == 0)

			printf("\n");

		randomtotal += randomnumbers[i];
	}
	randomaverage = randomtotal / 10;

	printf("%lf", randomaverage);

	system("pause");
}
Hello Baps,

What would be the equivalent to std::cout << std::fixed << std::showpoint << std::setprecision(2); for me?
Actually this is the equaliviant of your printf statements. The format specifiers in the printf take care of how the numbers are printed. "fixed" means to use regular numbers not scientific notation. "showpoint" means to show zero(s) to the right of the decimal point. And "setpercision" says how any numbers there are to the right of the decimal point.

Here is what I'm looking at now. I'm getting lots of errors regarding static_cast and size_t. Are these normal variables in c?
No these are C++ standard names. "static_cast" may even be C++11 standards I am not sure. "size_t" may be a C++ name and it is just another name for "unsigned int".

My compiler is set to use the C++11 standards and that is why it does not give me any errors or warnings.

Your compiler may not be set for the C++11 standards or it might be an older compiler for C programs.

For line 12 you could use srand((unsigned int)(time(0));. It has been awhile, but I believe this is the C way of type casting a variable or in this cas a return value.

I do not know what your compiler is or how it is set up, but it might not hurt to see if the is a newer version the would at least use the 2011 standards.

BTW even your original program with the change to line 12 compiled and ran fine for me.

Hope that helps,

Andy
Last edited on
Thanks for all your help, Andy! My compiler wasn't recognizing some of the prior changes, now it works fine! Thanks so much for all your help!
Topic archived. No new replies allowed.