[try Beta version]
Not logged in

 
what is this

Pages: 12
Dec 10, 2014 at 10:19pm
Create an array of integers in the main program then call a function that prints out the array (you need to pass the array to the function)

anyone ever do that before?

Dec 10, 2014 at 10:27pm
Pretend no one ever has and give it a shot...
Dec 10, 2014 at 10:28pm
This sounds like a homework/lab program in a first programming course.
I don't know if you've learned about pointers, so I won't go into that. I didn't learn about pointers until a long time after I learned about arrays.

An array is a bunch of variables (of the same type) stored in (essentially) one variable, but with indexes. It's a little more complicated than that, but that's fine for now.

The function you're passing it to NEEDS to know that what you're sending it is an array. So, we can't just pass it like a normal variable - we have to include the "[]" when we pass the array to the function.
Dec 10, 2014 at 10:32pm
what kind of program would have to be made?
Dec 10, 2014 at 11:41pm
You should give it a try :o I'm new at c++ as well but I would try something along the lines of this:

function getArray(whatever array here (needs pointer I believe));

int main(){

create array blahblahblah[]

call function getArray

return 0;
}

function getArray{
for loop(appropriate parameters){
cout integers in array
}

Like I said, I am no expert. But that is what comes to mind when I read your problem. I would just type it out and experiment. Good luck! :D
Dec 11, 2014 at 1:25am
so for the array would it be the same as typing in

(int num,int 1,int 2)
Dec 11, 2014 at 1:25am
and essentially this needs a program right?
Dec 11, 2014 at 6:23pm
bump
Dec 11, 2014 at 8:29pm
array of 10 integers:
int myArray[10]

function that prints the array
1
2
3
4
5
void printArray(int someArray[], int size){
for(int i = 0; i < size; i++){
cout << someArray[i] << endl;
}
}


Pass the array to the function:
printArray(myArray, 10);

This is a lot of help, you should be able to do the rest your self.
And by the rest i mean implementing this into your program.
Last edited on Dec 11, 2014 at 8:32pm
Dec 11, 2014 at 10:19pm
so could i just make my program and input that scenario into this example

#include <iostream>
#include <cmath>

int main ()
using namespace std;

int num[1];
num [0]=9;

cout << num[1]+num[0];


system("pause");
return 0;
}

very similar to that right?

im so lost and this is due tonite
Last edited on Dec 11, 2014 at 11:45pm
Dec 12, 2014 at 12:23am
any help is appreciated this is due for me at midnight
Dec 12, 2014 at 12:49am
You need to create a function that prints out the array as the assignment says.

Create an array of integers in the main program then call a function that prints out the array (you need to pass the array to the function)


What you did was create an array of 2 integers, and then print them out in main.
Last edited on Dec 12, 2014 at 12:54am
Dec 12, 2014 at 1:06am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;
// Function definition.
void printArray(int someArray[], int size){
    for(int i = 0; i < size; i++){
        // Print out the value held by the element corresponding to i.
        cout << someArray[i] << endl; 
   }
}


int main()
{
    int myArray[10]; // Remember to assign values to each element.
    printArray(myArray, 10); // Pass the array to the function and the size of the array.

    return 0;
}
Last edited on Dec 12, 2014 at 1:08am
Dec 12, 2014 at 1:48am
would i have to change anything out of that?

it out puts this

1
0
4233689
0
3
0
51
0
1992037728
0
Dec 12, 2014 at 1:49am
i honeslty do appreciate the help jacobhaha
Dec 12, 2014 at 2:04am
I didn't assign values to it. So whatever it prints out is undefined.

Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;
// Function definition.
void printArray(int someArray[], int size){
    for(int i = 0; i < size; i++){
        // Print out the value held by the element corresponding to i.
        cout << someArray[i] << endl; 
   }
}


int main()
{
    int myArray[10] = {0, 1, 2, 4, 5, 6 , 7, 8, 9}; // Remember to assign values to each element.
    printArray(myArray, 10); // Pass the array to the function and the size of the array.

    return 0;
}
Last edited on Dec 12, 2014 at 2:07am
Dec 12, 2014 at 2:06am
well that array output to 9 is solved.

#include <iostream>
using namespace std;

int main(void){
int arr[10]={0,1, 2, 3, 4 ,5 ,6 ,7 ,8, 9};
for(int p=0;p<10;p++)
cout << arr[p] << " " ;
return 0;
}

with that.
Dec 12, 2014 at 2:07am
im sorry im just so pressed for time with just 3 hours left.
Dec 12, 2014 at 2:08am
Okay.
Dec 12, 2014 at 2:18am
would that be all there is to do with that program or what
Pages: 12