Ouputting values from array

Good day everyone, thank all of you for your support, this has been the best site to improve my skills. I have this particular issue. Im required to create a function that will allow an array to be populated, then another function should output the values stored in that array.

This is my code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>
#include <vector>
using namespace std;

// Function for populating array:
int InArr () {
	
	int arr [5];
    
    cout << "\t You will be requested to enter 5 values but wont output them";
    
    for ( int i = 0; i < 5; i++ ) {
    	
    	cout << "\tEnter a number: ";
    	cin >> arr [i];
	} cout << endl;   
}

void outArr( int a [5] ) {
	
	for (int i = 0; i < 5; i++ ) {
		
		cout << a [i] << "  ";
	} cout << endl << endl;	
}


int main () {

outArr(InArr());

		
}


these are the follwing erros:

30 14 C:\Users\KingShaq\Desktop\Assignment 1 q6.cpp [Error] invalid conversion from 'int' to 'int*' [-fpermissive]

19 6 C:\Users\KingShaq\Desktop\Assignment 1 q6.cpp [Note] initializing argument 1 of 'void outArr(int*)'

I would gladly appreciate your help
By the way, I'm pretty sure you're doing it a bit differently than you were asked to do but I will first explain how to modify your current program.

Your function int InArr () {...} does not have a return statement. Functions that have a return type other than void MUST have a (valid) return statement.

So changing that function to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int* InArr() {

	int arr[5];

	cout << "\t You will be requested to enter 5 values but wont output them";

	for (int i = 0; i < 5; i++) {

		cout << "\tEnter a number: ";
		cin >> arr[i];
	} cout << endl;

	return arr;
}


should work.

int* specifies that we're returning a pointer to integer. In C++ you cannot return C-style arrays by value, so we have to return a pointer. But it's the same thing though, in use, so treat it like a regular array.

However, I think what you were asked to do is make a function that takes an array as parameter and populate it, not make a function to both create and populate an array. Your displayin' function is just fine though.

If you create an array in the function itself then you have to assign something with it otherwise it will just cease to exist after the function finishes executing. So you can instead take an array as parameter (would make sense to pass the array by reference, then you do not need to reassign) and populate it.


Thank you, but it did not work. I dont know why. But is fine. I gave up on this question.. Thanks for your code. Ill research more about this particular question.... :)
The reason it didn't work is because InArr is returning a pointer to a local array.

When the function exits, the array disappears as well, and all you're left with is a pointer to memory you no longer own.

At best, you print garbage.
At worst, the OS kills your program for stepping where it shouldn't.

The code was an example of how your code should be modified if it were to do your approach. But I don't recommend doing that at all as I said in that post.

The reason the code snippet didn't work is because you're supposed to assign the function's return to another array, because, like salem said, the array is local. I was just showing how your code would be modified to make it valid.

You don't want the function to "make" the array. You want to declare an array in the main function and pass it by reference to the function. If you have not learnt about passing by reference then you must pass the array and then assign the same array with the return value of the function.



Topic archived. No new replies allowed.