what is this

Pages: 12
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?

Pretend no one ever has and give it a shot...
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.
what kind of program would have to be made?
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
so for the array would it be the same as typing in

(int num,int 1,int 2)
and essentially this needs a program right?
bump
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
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
any help is appreciated this is due for me at midnight
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
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
would i have to change anything out of that?

it out puts this

1
0
4233689
0
3
0
51
0
1992037728
0
i honeslty do appreciate the help jacobhaha
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
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.
im sorry im just so pressed for time with just 3 hours left.
Okay.
would that be all there is to do with that program or what
Pages: 12