[University - C++ Programming] Filling an Array with RNG & Display Contents
I'm back again with some more C++ questions. This time it's a little easier, it's as simple as filling an array with 20 random numbers and then displaying them to the screen. Most of the code has been provided by my teacher. I'm pretty sure I can get the RNG to work, I'm just getting a few confusing errors.
This is the exercise: Quote:
Code:
#include <iostream> My second error is the fact that I don't understand anything beyond the line starting with Display(randArray, sizeof(.....) . If someone could explain this to me in the most simple possible way, I'd very much appreciate it. As for the third error, well there isn't one yet but I'm sure some problem will arise. If you could tell me what's correct, what needs tweaking and what is completely wrong, it should hopefully guide me in the right direction. Again, I'm willing to pay credits for relevant help, just let me know if you want any once I'm done with the exercise. Thanks for any help in advance. |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Your current code doesn't make any sense. The first time you pass through the "for" block the statement "return 0" will be called and finish your program, the function Display should be defined outside of the main block and the global definition of randArray and i are "shadowed" by the new (and independent) re-definitions inside the main block.
I'm assuming you missed my last post in your other topic, you should definitely check the tutorial with all the language basic constructs and what they actually mean. The answer to your first error is there too. For the second error (that is actually a question), I'll point you to this. I'm assuming you understand functions, how pointers work and that you can reference arrays as pointers (if you don't, refer to the tutorial in the other post). |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Ok before we go any further, I can't even run my program because of this error:
Quote:
Suggestions on how to get around this? EDIT - Working now. No idea what the problem was. |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Lol you've probably figured it out by now, but...
* I'd try closing all instances of the program already running (maybe you're trying to write to the file when it's in use) * Open that path in your file system, and check if the file is actually there. * Close VS and reopen it... lol out of ideas >< * Open VS as Administrator... really out of ideas t.t * Restart the computer... okay now idk. Good luck with your CS hw, though :D |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Cheers for that llyair, got it working now. Closed down VS and opened it again and it worked. Weird cause I tried that earlier and it didn't work.
Alright so now I'm pretty sure I've got values stored in all 20 elements of the array. This is code atm, I've chopped off the end bit just to ensure that the array was functioning properly. Code:
#include <iostream> |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
D:
strange, hopefully it doesn't happen again haha, it would probably be annoying. buuut i'm glad you got it working now! glgl |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
This is what the teacher provided in terms of the second half of the program. I think it's asking to display the contents of each element in the array. I'm just trying to work out what the first line means (the Display function). I'm fairly certain I don't need to change anything in void Display(int *arrayData,...), but please let me know otherwise.
Code:
Display(/* Insert your function identifier here */, sizeof(/* Insert your Code:
Display(int randArray, sizeof(int randArray) / sizeof(int)); |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
You clearly have no idea how functions work. About actually using the function you need to know the relationship between arrays and pointers.
Have a nice read. EDIT: The random values initialization part and printing is correct. |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Quote:
Appreciate the links and the feedback on the RNG. I'll have a thorough read and get back to this thread when I'm completely stuck again. |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
generic mockery about rand being garbage even though it doesnt matter at all in this case.
|
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
btw: the first instance of Display() is a prototype, the second is the call from main, and the third is the actual declaration.
|
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
also i'm not sure what your teacher's policy on
Code:
using namespace std |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Quote:
Quote:
http://www.cprogramming.com/declare_vs_define.html It also has a really simplified example, but it's similar to what you have with Display() showing up three times. Just copying the code here for convenience: Code:
int func(); |
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Alright I've got it all to work. The line that I was having trouble with was really simple (I knew I was over thinking it) and I actually had the answer a long time ago, just VS said there was an error (when there wasn't) and all I needed to do was re-build the solution. Hopefully VS co-operates with me next time :P thanks all for your help, once again it's much appreciated. If you guys want some credits for the time you spent explaining things to me, just post in this thread or PM me and I'll happily give you some.
Quote:
Quote:
Quote:
|
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Quote:
Code:
namespace Foo Code:
// Now you can clearly see which function is being called. Nice! Code:
namespace Engine EDIT: Oh! As a sidenote, if you find yourself stuck on a problem for a long time http://www.stackoverflow.com is a great reference for asking or searching for common questions. Also, if you're ever unsure of what a standard function does, http://en.cppreference.com/w/ has you covered :) |
All times are GMT -5. The time now is 12:07 AM. |
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution