Having trouble understanding sub-program concept
I have to make a sub-program like this:
int DonnerVecteur(double *vecteur, int nbValeurs)
{
vecteur = new double* [nbValeurs];
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i] = (1 + rand()%50) + 5;
cout << vecteur[i];
}
return(0);
}
And then I need to call it in my main():
void main()
{
DonnerVecteur(0,20);
}
Now I have difficulty understanding the concept of sub-programs.
For example: I am declaring a array (double *vecteur)... but when I call my sub-program, I can't give this array a value of 0... it's not even created yet.
Also, do I absolutely need a "return();" in my sub-program?
Thx in advance
# 1 Re: Having trouble understanding sub-program concept
For example: I am declaring a array (double *vecteur)... but when I call my sub-program, I can't give this array a value of 0... it's not even created yet.Why do you create the array within the function? Why not create the array before you call the function and pass it using the variable name.
Also, do I absolutely need a "return();" in my sub-program?That depends on whether you have delcared a return type.
# 2 Re: Having trouble understanding sub-program concept
Why do you create the array within the function? Why not create the array before you call the function and pass it using the variable name.
That depends on whether you have delcared a return type.
What do you mean by declaring a return type? If I don't want a return I have to declare my sub-program void?
Even if I create the array before I call the function, it has to be filled in my function...
So I'd have something like this:
int DonnerVecteur(int vecteur[20], int nbValeurs)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i] = (1 + rand()%50) + 5;
cout << vecteur[i];
}
return(0);
}
void main()
{
DonnerVecteur(0,20);
}
Which doesn't work any better. Gives me an error: "Program stopped working" (I'm using vista).
Oh and do I HAVE to give my array a certain size? Or can I declare it as "int vecteur[]".
# 3 Re: Having trouble understanding sub-program concept
What do you mean by declaring a return type? If I don't want a return I have to declare my sub-program void?Correct.
The point I was trying to make is that your original funciton is expecting a pointer. You should not pass null as a pointer. Are you trying to get the vector back from the function?
# 4 Re: Having trouble understanding sub-program concept
Correct.
The point I was trying to make is that your original funciton is expecting a pointer. You should not pass null as a pointer. Are you trying to get the vector back from the function?
The function just needs to fill a 20 value vector with random values between 5 and 55.
# 5 Re: Having trouble understanding sub-program concept
The function just needs to fill a 20 value vector with random values between 5 and 55.Then, if you know you will always have 20 elements, I would declare it initially and pass it through to the function. That way, you are passing a valid pointer. Something like...
double vecteur[20];
DonnerVecteur(vecteur,20);
# 6 Re: Having trouble understanding sub-program concept
You mean like this?
int DonnerVecteur(int vecteur[20], int nbValeurs)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i] = rand()%50 + 5;
cout << vecteur[i] << endl;
}
return(0);
}
void main()
{
int vecteur[20]
DonnerVecteur(vecteur,20);
}
Alright, thanks! This code works perfectly!
But I have another question though... is there a way to declare vecteur[20] only once? Because now it's declared once in main and another time in DonnerVecteur
# 7 Re: Having trouble understanding sub-program concept
Because I need a second function to "cout" my vector. So I would need it to be declared for all my functions...
# 8 Re: Having trouble understanding sub-program concept
is there a way to declare vecteur[20] only once? Because now it's declared once in main and another time in DonnerVecteurWhere is it defined in the function? The function just declares that it expects an array of 20 ints as the first parameter.
# 9 Re: Having trouble understanding sub-program concept
Where is it defined in the function? The function just declares that it expects an array of 20 ints as the first parameter.
Yeah sorry, I wasn't really clear... I'm not all that good in english.
I meant to ask if there's a way to make this vector accessible for all my functions. Because this is what my final program will look like:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int DonnerVecteur(int vecteur[20][2], int nbValeurs)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i][j] = rand()%50 + 5;
}
}
void AfficherVecteur(int nbValeurs)
{
int i = 0;
cout << "Le" << j + 1 << "e vecteur genere est: (";
for (i=0; i<nbValeurs; i++) {
cout << vecteur[i][j] << ", ";
}
cout << ")";
}
double MultiplierScalaire(int vecteur[20][0], int vecteur2[20][1], int nbValeurs)
{
int resultat = 0;
for (i=0; i<nbValeurs; i++) {
resultat = resultat + (vecteur[i][0] * vecteur[i][1];
}
return(resultat);
}
void main()
{
int vecteur[20][2] = {0};
int j = 0;
DonnerVecteur(vecteur,20);
AfficherVecteur(20);
j = 1
DonnerVecteur(vecteur,20);
AfficherVecteur(20);
}
So I generate and "cout" two random vectors. Then I have to multiply them together in my "MultiplierScalaire" function.
I need "j" and both my generated vectors to be accessible in all my functions... how do I do this?
# 10 Re: Having trouble understanding sub-program concept
But I have another question though... is there a way to declare vecteur[20] only once? Because now it's declared once in main and another time in DonnerVecteur
you are not declaring it twice, but you can write:
int DonnerVecteur(int *vecteur, int nbValeurs)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i] = rand()%50 + 5;
cout << vecteur[i] << endl;
}
return(0);
}
void main()
{
int vecteur[20]
DonnerVecteur(vecteur,20);
}
then the function can be called with any size array - as long as the nbValeurs value is correct as well
i.e.:
void main()
{
int vecteur[20]
DonnerVecteur(vecteur,20);
int differentArray[100]
DonnerVecteur(differentArray,100);
}
# 11 Re: Having trouble understanding sub-program concept
You can declare it globally, or, pass it to every function that needs it.
# 12 Re: Having trouble understanding sub-program concept
Alright! I got everything working... or almost
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int vecteur[20][2];
int j=0;
void DonnerVecteur(int vecteur[20][2], int nbValeurs)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i][j] = rand()%50 + 5;
}
}
void AfficherVecteur(int nbValeurs)
{
int i = 0;
cout << "Le " << j + 1 << "e vecteur genere est: (";
for (i=0; i<nbValeurs; i++) {
if(i < 19) {
cout << vecteur[i][j] << ", ";
}
else {
cout << vecteur[i][j];
}
}
cout << ")" << endl;
}
double MultiplierScalaire(int vecteur[20][2], int nbValeurs)
{
int resultat = 0, i = 0;
for (i=0; i<nbValeurs; i++) {
resultat = resultat + (vecteur[i][0] * vecteur[i][1]);
}
return(resultat);
}
void main()
{
int resultat = 0;
DonnerVecteur(vecteur, 20);
AfficherVecteur(20);
j = 1;
DonnerVecteur(vecteur, 20);
AfficherVecteur(20);
resultat = MultiplierScalaire(vecteur, 20);
cout << "Le resultat de la multiplication scalaire de ces 2 vecteurs est: " << resultat << endl;
}
Why do vecteur[][0] and vecteur[][1] have the exact same values?
# 13 Re: Having trouble understanding sub-program concept
Alright! I got everything working... or almost
Why do vecteur[][0] and vecteur[][1] have the exact same values?
i just did it as well, and yes they are the same
i think because the system time is the same, maybe you need to put a 1 second pause?
here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void DonnerVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i][j] = rand()%50 + 5;
}
}
void AfficherVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
cout << "Le" << j + 1 << "e vecteur genere est: (";
for (i=0; i<nbValeurs; i++) {
cout << vecteur[i][j] << ", ";
}
cout << ")\n\n";
}
double MultiplierScalaire(int vecteur[20][2], int nbValeurs)
{
int resultat = 0;
for (int i=0; i<nbValeurs; i++) {
resultat += (vecteur[i][0] * vecteur[i][1]);
}
return(resultat);
}
void main()
{
int vecteur[20][2] = {0};
DonnerVecteur(vecteur,20,0);
AfficherVecteur(vecteur,20, 0);
DonnerVecteur(vecteur,20, 1);
AfficherVecteur(vecteur,20, 1);
cout << "resultat = " << MultiplierScalaire(vecteur, 20) << "\n";
}
# 14 Re: Having trouble understanding sub-program concept
Yeah that's exactly what I though... how do I put a 1 second pause?
# 15 Re: Having trouble understanding sub-program concept
How do I fix that? I need two random and DIFFERENT vectors...
this works for me:
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void DonnerVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
srand((unsigned)time(NULL));
for (i=0; i<nbValeurs; i++) {
vecteur[i][j] = rand()%50 + 5;
}
}
void AfficherVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
cout << "Le" << j + 1 << "e vecteur genere est: (";
for (i=0; i<nbValeurs; i++) {
cout << vecteur[i][j] << ", ";
}
cout << ")\n\n";
}
double MultiplierScalaire(int vecteur[20][2], int nbValeurs)
{
int resultat = 0;
for (int i=0; i<nbValeurs; i++) {
resultat += (vecteur[i][0] * vecteur[i][1]);
}
return(resultat);
}
void main()
{
int vecteur[20][2] = {0};
DonnerVecteur(vecteur,20,0);
AfficherVecteur(vecteur,20, 0);
Sleep(1000);
DonnerVecteur(vecteur,20, 1);
AfficherVecteur(vecteur,20, 1);
cout << "resultat = " << MultiplierScalaire(vecteur, 20) << "\n";
}
# 16 Re: Having trouble understanding sub-program concept
I get:
1>c:\users\alex\desktop\ecole\inf1005c\tds\td4\solution_td4\solution_td4\exo1.cpp(14) : fatal error C1083: Cannot open include file: 'windows.h': No such file or directory
Is this because I'm using Visual C++?
# 17 Re: Having trouble understanding sub-program concept
I get:
1>c:\users\alex\desktop\ecole\inf1005c\tds\td4\solution_td4\solution_td4\exo1.cpp(14) : fatal error C1083: Cannot open include file: 'windows.h': No such file or directory
Is this because I'm using Visual C++?
no, i'm using Visual C++ too. you just need to add the right path to your project.
failing that, you could add a bit more randomness to your number seed by using some other numbers as well. the 1 second pause is not very elegant anyway.
why not just multiply it by (j+1) :)
[j is no good on its own since it is always zero the first time]
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void DonnerVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
srand((unsigned)time(NULL)*(1+j));
for (i=0; i<nbValeurs; i++) {
vecteur[i][j] = rand()%50 + 5;
}
}
void AfficherVecteur(int vecteur[20][2], int nbValeurs, int j)
{
int i = 0;
cout << "Le" << j + 1 << "e vecteur genere est: (";
for (i=0; i<nbValeurs; i++) {
cout << vecteur[i][j] << ", ";
}
cout << ")\n\n";
}
double MultiplierScalaire(int vecteur[20][2], int nbValeurs)
{
int resultat = 0;
for (int i=0; i<nbValeurs; i++) {
resultat += (vecteur[i][0] * vecteur[i][1]);
}
return(resultat);
}
void main()
{
int vecteur[20][2] = {0};
DonnerVecteur(vecteur,20,0);
AfficherVecteur(vecteur,20, 0);
DonnerVecteur(vecteur,20, 1);
AfficherVecteur(vecteur,20, 1);
cout << "resultat = " << MultiplierScalaire(vecteur, 20) << "\n";
}
# 18 Re: Having trouble understanding sub-program concept
Thanks A LOT both of you!
Everything is working right now.
If ever I wanted to add the right path to my project for windows.h, how would I do this?
# 19 Re: Having trouble understanding sub-program concept
you can use Sleep(1000); to wait one second. However I would just call srand() once at the beginning of the code to avoid such a fix.
# 20 Re: Having trouble understanding sub-program concept
Yeah, multiple calls to srand is counterproductive. Better to avoid the Windows dependency on Sleep().
And FYI, while there are some valid reasons for using global variables, it's not good form to do so if you can avoid it. Doing so puts beginners in the wrong state of mind. That's why many introductory courses forbid it entirely.