[try Beta version]
Not logged in

 
Arrays within a function

Oct 22, 2020 at 5:59pm
Hello, im being asked to create a user defined array. Ive gotten that code down, but im having trouble defining the array within a call function. If anyone knows how to properly code an array within a call function that would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  int main() {
int i = 0, n = 0;
  float arr[100];

cout << "Enter total number of elements(1 to 100): ";
  cin >> n;
  cout << endl;
  

  for(i = 0; i < n; ++i)
  {
  cout << "Enter Number " << i + 1 << " : ";
  cin >> arr[i];
  }

  for(i = 1;i < n; ++i)
  {
   
   if(arr[0] > arr[i])
   arr[0] = arr[i];
  }
  cout << "Smallest Element = " << arr[0];
return 0;
Oct 22, 2020 at 6:04pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int smallest_element(int array[], int size) {
	int value = array[0];
	for (i = 1; i < size; ++i) {
		if (value > array[i])
			value = array[i];
	}
	return value;
}

int main(){
	//...
	int smallest = smallest_element(arr, n);
	std::cout << "Smallest Element = " << smallest << '\n';
}
¿what are you trying to do that is giving you issues?
Oct 22, 2020 at 6:23pm
Im having trouble calling the size that the user wants for the array and then the elements of the array into the smallest_element function.
Oct 22, 2020 at 9:23pm
misread that...
calling the size? What does it mean?
its just how many locations the array has, or how many are in use, depending on what you are talking about. If 10 locations are in use, its 10.

Last edited on Oct 22, 2020 at 9:25pm
Oct 23, 2020 at 8:02am
yeah my terminology with c++ is trash. Still new to the language and how to correctly get my point across. Anyways I got my code to run the way I needed it to.
Topic archived. No new replies allowed.