|
|
Thread Tools | Display Modes |
09-10-2015, 07:47 AM | #1 | |
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
[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> #include <cstdlib> #include <ctime> #include <time.h> #include <stdlib.h> using namespace std; using std::srand; using std::rand; using std::time; int randArray[20]; int i; void Display(int *, int); int main(void) { int randArray[20]; srand(time(NULL)); for (int i = 0; i < 20; i++) { randArray[i] = rand() % 20 + 1; // The following 4 lines of code were to check the values entered into array elements. I will remove it when I'm confident that the values are correct. cout << randArray[1] << endl; cout << randArray[2] << endl; cout << randArray[3] << endl; cout << randArray[20] << endl; Display(randArray, sizeof(randArray[1]) / sizeof(int)); return 0; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; } } 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.
__________________
|
|
09-10-2015, 11:31 AM | #2 |
Under the scarlet moon
Join Date: Jan 2014
Age: 31
Posts: 921
|
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). |
09-10-2015, 11:12 PM | #3 | |
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
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.
__________________
Last edited by sickufully; 09-11-2015 at 12:14 AM.. |
|
09-11-2015, 12:10 AM | #4 |
Wiki Staff
Join Date: Jun 2014
Posts: 307
|
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 |
09-11-2015, 12:15 AM | #5 |
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
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> #include <cstdlib> #include <stdlib.h> #include <ctime> using namespace std; using std::srand; using std::rand; using std::time; void Display(int *, int); int main(void) { int randArray[20]; srand( time( NULL ) ); for (int i = 0; i < 20; i++) { randArray[i] = rand() % 20 + 1; } cout << randArray[0] << endl; cout << randArray[1] << endl; cout << randArray[2] << endl; cout << randArray[19] << endl; }
__________________
Last edited by sickufully; 09-11-2015 at 12:24 AM.. |
09-11-2015, 12:24 AM | #6 |
Wiki Staff
Join Date: Jun 2014
Posts: 307
|
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 |
09-11-2015, 04:14 AM | #7 |
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
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 function identifier here */)/sizeof(int)); return 0; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; } Code:
Display(int randArray, sizeof(int randArray) / sizeof(int)); return 0; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; }
__________________
|
09-11-2015, 06:48 AM | #8 |
Under the scarlet moon
Join Date: Jan 2014
Age: 31
Posts: 921
|
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. Last edited by AutotelicBrown; 09-11-2015 at 06:53 AM.. |
09-11-2015, 07:06 AM | #9 | |
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
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.
__________________
|
|
09-11-2015, 11:13 PM | #10 |
The Worst
|
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.
__________________
|
09-11-2015, 11:19 PM | #11 |
The Worst
|
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.
__________________
|
09-11-2015, 11:26 PM | #12 |
The Worst
|
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
__________________
|
09-12-2015, 12:23 AM | #13 | ||
Wiki Staff
Join Date: Jun 2014
Posts: 307
|
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(); int main() { int x = func(); } int func() { return 2; } |
||
09-13-2015, 07:08 PM | #14 | ||
Sir Krisk
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
|
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:
This was a major help, especially when I read over the link that llyair provided, I'm pretty sure a few things clicked.
__________________
|
||
09-15-2015, 02:23 AM | #15 | |
Wubalubadubdub
Join Date: May 2005
Age: 36
Posts: 1,697
|
Re: [University - C++ Programming] Filling an Array with RNG & Display Contents
Quote:
Code:
namespace Foo { void Func() { // Does something! } } namespace Bar { void Func() { // Does something completely different! } } using namespace Foo; using namespace Bar; Func(); // Error! Can't figure out which one to call Code:
// Now you can clearly see which function is being called. Nice! Foo::Func(); Bar::Func(); Code:
namespace Engine { namespace Tools { namespace UI { void Foo(); } } } // This is quite a pain to have to type EVERY single time you need this function... Engine::Tools::UI::Foo(); 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
__________________
boop Last edited by shenjoku; 09-15-2015 at 02:27 AM.. |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|