[C++ Programming Question]
I tried to find my middle value in my array. So, I tried to use
sizeof(array) / sizeof(array[0])
but it is not working as well
Could you help me to fix it?
My middle value function located at void middleValue(int array[]) << here
Here is my code
#include
#include // Input/Output with Files , ifstream(read from file) + ofstream(write on file)
using namespace std;
class Random
{
private:
public:
void allValues(int array[])
{
cout << "Show all values" << endl;
for(int i =0; i < 1000; i++)
{
cout << array[i] << endl;
}
}
void sumValues(int array[])
{
cout << "Output the sum of all values" << endl;
int sum = 0;
for(int i = 0; i < 1000; i++)
{
sum += array[i];
}
cout << sum < highValue)
{
highValue = array[i];
}
}
cout << highValue < array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout << "Output all values thar sorted in Bubble sort" << endl;
for(int k = 0; k < 1000; k++)
{
cout << array[k] << endl;
}
}
void linearSearch(int array[])
{
int searchValue, flag;
cout << "Input what values do you want to find: " << endl;
cin >> searchValue;
flag = 0;
for(int i = 0; i < 1000; i ++)
{
if(array[i] == searchValue)
{
cout << searchValue << "is located at" << i+1 << endl;
flag = 1;
//return;
}
}
if(!flag)
{
cout << searchValue << "is not exsited in array" << endl;
}
}
};
int main() {
int ranNum; //random number
const int arraySize = 1000; // Fixed array size
int array[arraySize];
int choice; // Used in Menu Part
ofstream outfile; //Write on File
outfile.open("randomfile");
srand(time(0)); // producing a series of pseudo-random integers
/**If we generate a sequence of random number with rand() function,
it will create the same sequence again and again every time program runs
*/
cout << "Writing data to file..." << endl;
for (int i = 0; i < 1000; i++) {
ranNum = (rand() % 1000) + 1;
//cout << ranNum << "\n";
outfile << ranNum << "\n";
}
outfile.close();
ifstream file;
file.open("randomfile");
file.seekg(0, ios::beg);
cout << "Reading data from file..." << endl;
for (int i = 0; i < 1000; i++) {
file >> array[i]; // Push into my array
}
file.close();
cout << "===========================================================" << "\n";
Random rd; // random rd is not working wtf?
/**
* Here while passing the array as parameter, we just need to send
* the name of the array without its size
*/
do
{
cout << "1.Show All Values" << endl;
cout << "2.Show Sum of All Values" << endl;
cout << "3.Show All Odd Values" << endl;
cout << "4.Show All Even Values" << endl;
cout << "5.Show Middle Value" << endl;
cout << "6.Show First Element value" << endl;
cout << "7.Show Last Element Value" << endl;
cout << "8.Show Highest Value" << endl;
cout << "9.Show Lowest Value" << endl;
cout << "10.Sorting array by using bubble sort" << endl;
cout << "11.Finding value by using Linear Search" << endl;
cout << "12.Terminate Program" << endl;
cout << "Input Your Choice: " << endl;
cin >> choice;
switch(choice)
{
case 1:
rd.allValues(array);
break;
case 2:
rd.sumValues(array);
break;
case 3:
rd.oddValue(array);
break;
case 4:
rd.evenValue(array);
break;
case 5:
rd.middleValue(array);
break;
case 6:
rd.firstValue(array);
break;
case 7:
rd.lastValue(array);
break;
case 8:
rd.highestValue(array);
break;
case 9:
rd.lowestValue(array);
break;
case 10:
rd.sortValue(array);
break;
case 11:
rd.linearSearch(array);
break;
case 12:
cout << "Terminate Program" << endl;
}
}while(choice != 12);
return 0;
}
void middleValue(int array[])
{
int size=sizeof(array);
cout<<size;
}
When the above function was called it always prints 8 .
Because sizeof(arr) is returning the size of a pointer variable.
1) sizeof() returns the size of the array when the base address of an array is passed to it.
2 )But you're declaring " array[] " in the function definition ------> void middleValue(int array[]) .
3) void middleValue(int array[]) is actually equals to the void middleValue(int *array)
In the function, array doesn't act as a base address instead it acts as a pointer to an array.
As it was an array, sizeof() is returning the size of a pointer varaible.
I hope you understand this.....If you do not understand, feel free to comment.
If you like my work please give me thumbs up![Please don't forget:(]
[C++ Programming Question] I tried to find my middle value in my array. So, I tried...