Home > Back-end >  C global variable problem
C global variable problem

Time:04-01



# include & lt; iostream>
using namespace std;

Int size=5;

Void selectionSort (int arr [])
{
For (int k=1; K & lt; The size; K++)
{
Int temp=arr [k].
Int j=k - 1;
While (j & gt;=0 & amp; & Temp & lt;=arr [j])
{
Arr=arr [m + 1] [j];
J=j - 1;
}
arr[j + 1]=temp;
}
}

Void displayArray (int arr [])
{
//int size=5;
for (int i=0; I & lt; The size; I++)
{
Cout & lt; <"" & lt; }
}

Int main ()
{
//int arr [5].
Int arr [size];
Cout & lt; <"Both Please enter 5 number to the array." & lt;
for (int i=0; I & lt; The size; I++)
{
Cin & gt;> Arr [I];
}

for (int i=0; I & lt; The size; I++)
{
Cout & lt; <"" & lt; }

SelectionSort (arr);
Cout & lt; <"\ nafter sorting" & lt; DisplayArray (arr);


return 0;
}

Would like to ask why I have the statement below global variables each function can't use the size?

CodePudding user response:

This is where the global variables of embarrassment,
You'd better think a unique variable names,
As for the size, already with the jam file, so can go wrong,

CodePudding user response:

To compile the results posted

CodePudding user response:

Const int size=5;
So it is good

CodePudding user response:

Because the size is not constant, cannot use array definition, to add const

CodePudding user response:

So try to change the
 const int size=5; 

CodePudding user response:

Upstairs said to want to use very amount then it is an array by new char [size] this form
  • Related