Go Back   Flash Flash Revolution: Community Forums > General Discussion > Critical Thinking > Homework & Help

Reply
 
Thread Tools Display Modes
Old 09-10-2015, 07:47 AM   #1
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default [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:
For this task you are required to create an array of 20 integers, which you will populate with a random number with a value between 1 and 20.
Once you have created this array of numbers, you will then need to go through and display the value of each of the array elements.
Here is my code so far:
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 first error is the fact that the only number being entered into the array is -883XXXXXX. Basically some huge random number and I don't understand where it's coming from.

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.
__________________
sickufully is offline   Reply With Quote
Old 09-10-2015, 11:31 AM   #2
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 26
Posts: 812
Default 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).
AutotelicBrown is offline   Reply With Quote
Old 09-10-2015, 11:12 PM   #3
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default 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:
Error: cannot open file 'C:\Users\admin\documents\visual studio 2015\Projects\Task6\Debug\Task6.exe'
It also mentioned that access is denied. It was running fine before I posted the OP.

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..
sickufully is offline   Reply With Quote
Old 09-11-2015, 12:10 AM   #4
llyair
Wiki Staff
FFR Wiki StaffFFR Veteran
 
llyair's Avatar
 
Join Date: Jun 2014
Posts: 307
Default 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
llyair is offline   Reply With Quote
Old 09-11-2015, 12:15 AM   #5
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default 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;
}
Am I correct in saying that I have successfully assigned a random number to all 20 elements of the array? AND printed out the values stored in elements 0, 1, 2 and 19?
__________________

Last edited by sickufully; 09-11-2015 at 12:24 AM..
sickufully is offline   Reply With Quote
Old 09-11-2015, 12:24 AM   #6
llyair
Wiki Staff
FFR Wiki StaffFFR Veteran
 
llyair's Avatar
 
Join Date: Jun 2014
Posts: 307
Default 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
llyair is offline   Reply With Quote
Old 09-11-2015, 04:14 AM   #7
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default 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;
}
This is what I have written (clearly isn't correct cause it doesn't run):
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;
	}
I'm confused by the fact that Display has been initialised before Main and then it's then called twice, but once within Main and once again outside of it. Can someone explain this to me? More along the lines of what does the first Display call do?
__________________
sickufully is offline   Reply With Quote
Old 09-11-2015, 06:48 AM   #8
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 26
Posts: 812
Default 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..
AutotelicBrown is offline   Reply With Quote
Old 09-11-2015, 07:06 AM   #9
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default Re: [University - C++ Programming] Filling an Array with RNG & Display Contents

Quote:
Originally Posted by AutotelicBrown View Post
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.
I understand how they work in the most basic terms possible (like the very first example given on the page you linked) but anything outside of that I struggle to comprehend. I've only been studying this for about 6 weeks among other subjects and I'm finding it REALLY difficult. There's just nothing clicking into place in my head so I'm constantly re-reading information about basic terms.

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.
__________________
sickufully is offline   Reply With Quote
Old 09-11-2015, 11:13 PM   #10
FoJaR
The Worst
 
FoJaR's Avatar
 
Join Date: Nov 2005
Location: space ghost is dead to me
Posts: 2,817
Send a message via AIM to FoJaR Send a message via MSN to FoJaR
Default 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.
__________________
FoJaR is offline   Reply With Quote
Old 09-11-2015, 11:19 PM   #11
FoJaR
The Worst
 
FoJaR's Avatar
 
Join Date: Nov 2005
Location: space ghost is dead to me
Posts: 2,817
Send a message via AIM to FoJaR Send a message via MSN to FoJaR
Default 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.
__________________
FoJaR is offline   Reply With Quote
Old 09-11-2015, 11:26 PM   #12
FoJaR
The Worst
 
FoJaR's Avatar
 
Join Date: Nov 2005
Location: space ghost is dead to me
Posts: 2,817
Send a message via AIM to FoJaR Send a message via MSN to FoJaR
Default 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
is but generally it is not the best idea
__________________
FoJaR is offline   Reply With Quote
Old 09-12-2015, 12:23 AM   #13
llyair
Wiki Staff
FFR Wiki StaffFFR Veteran
 
llyair's Avatar
 
Join Date: Jun 2014
Posts: 307
Default Re: [University - C++ Programming] Filling an Array with RNG & Display Contents

Quote:
Originally Posted by FoJaR View Post
also i'm not sure what your teacher's policy on
Code:
using namespace std
is but generally it is not the best idea
Agreed about the using namespace std, but I guess code style can be fixed after you have something running and understand it.

Quote:
Originally Posted by sickufully View Post
I'm confused by the fact that Display has been initialised before Main and then it's then called twice, but once within Main and once again outside of it. Can someone explain this to me? More along the lines of what does the first Display call do?
This page explains different uses of function names:
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;
}
See the section with this code, it'll explain the reason for func() showing up 3x.
llyair is offline   Reply With Quote
Old 09-13-2015, 07:08 PM   #14
sickufully
Fat Orange Cat
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 926
Default 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:
Originally Posted by llyair View Post
This page explains different uses of function names:
http://www.cprogramming.com/declare_vs_define.html
Appreciate the link man, cleared up some confusion I had. Definitely helped.

Quote:
Originally Posted by FoJaR View Post
also i'm not sure what your teacher's policy on
Code:
using namespace std
is but generally it is not the best idea
I have no idea why this is a bad idea (please enlighten me on why, curious to know) but that's just what the teacher supplied us with. I dunno if he did it for us c++ newbies or if he actually writes his code like that.

Quote:
Originally Posted by FoJaR View Post
btw: the first instance of Display() is a prototype, the second is the call from main, and the third is the actual declaration.
This was a major help, especially when I read over the link that llyair provided, I'm pretty sure a few things clicked.
__________________
sickufully is offline   Reply With Quote
Old 09-15-2015, 02:23 AM   #15
shenjoku
Wubalubadubdub
FFR Veteran
 
shenjoku's Avatar
 
Join Date: May 2005
Age: 32
Posts: 1,697
Default Re: [University - C++ Programming] Filling an Array with RNG & Display Contents

Quote:
Originally Posted by sickufully View Post
I have no idea why this is a bad idea (please enlighten me on why, curious to know) but that's just what the teacher supplied us with. I dunno if he did it for us c++ newbies or if he actually writes his code like that.
There's multiple reasons why using namespace whatever; is generally a bad idea. Consider this situation for a moment:
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
If you have functions with the same name in multiple namespaces it can cause great confusion and even compiler errors when you try to call those functions without the namepspace prefix. Now with the same example, if the using namespace statements are not used it would look like this:
Code:
// Now you can clearly see which function is being called. Nice!
Foo::Func();
Bar::Func();
It ultimately comes down to preference, but I've found that most programmers are against using namespace for the above reasons. The only times I've really seen it used are when there are a lot of nested namespaces like the following:
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();
Hopefully that's not too confusing XD Not sure how crazy I should get with these examples. I tried to keep it simple. Hope it helps!

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..
shenjoku is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution