Hello again,
i made some progress with this code, now the problem i face is everything works fine except for the 3rd option, i see both functions are written correctly and i'm calling them correctly,but when i press 3 i get nothing it's just null. what is wrong ?
#include <iostream>
usingnamespace std;
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void rvereseArr(int arr[], int first, int last)
{
if (first >= last)
return;
int temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
rvereseArr(arr,first +1,last -1);
}
int main()
{
char choice{};
int a[100];
int i,k,j,first,second,size;
while(choice != '4')
{
cout<<endl;
cout<<"1-Print the array"<<endl;
cout<<"2-Print the Second maximum number"<<endl;
cout<<"3-Print the reverse of the array using functions"<<endl;
cout<<"4-Return to the main menu."<<endl;
cin>>choice;
if(choice == '1')
{
cout<<"enter the size of the array \n";
cin>>size;
for(k=0; k<size;k++)
{
cout<<"enter the element "<< k + 1<<endl;
cin>>a[k];
}
cout<<"The array you entered is : "<<endl;
for(k=0;k<size;k++)
{
cout<<a[k];
cout<<" ";
}
}
elseif(choice == '2')
{
if (size < 2) // There should be at least two elements
{
cout<<" Invalid Input \n";
}
first = second = 0;
for (j=0;j<size;j++)
{
if (a[j] > first) // If current element is greater than first then update both first and second
{
second = first;
first = a[j];
}
if (a[j] > second && a[j] != first) // If arr[i] is in between first and second then update second
second = a[j];
}
if (second == 0)
cout<<"There is no second largest element\n";
else
cout<<"The second largest element is "<<second<<endl;
if(choice == '3')
{
rvereseArr(a,0,size-1);
cout<< "Reversed Array Elements are: \n";
printArray(a,size);
}
}
}
return 0;
}
#include <iostream>
usingnamespace std;
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void rvereseArr(int arr[], int first, int last)
{
if (first >= last)
return;
int temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
rvereseArr(arr, first + 1, last - 1);
}
int main()
{
char choice {
};
int a[100];
int i, k, j, first, second, size;
while (choice != '4') {
cout << endl;
cout << "1-Print the array" << endl;
cout << "2-Print the Second maximum number" << endl;
cout << "3-Print the reverse of the array using functions" << endl;
cout << "4-Return to the main menu." << endl;
cin >> choice;
if (choice == '1') {
cout << "enter the size of the array \n";
cin >> size;
for (k = 0; k < size; k++) {
cout << "enter the element " << k + 1 << endl;
cin >> a[k];
}
cout << "The array you entered is : " << endl;
for (k = 0; k < size; k++) {
cout << a[k];
cout << " ";
}
} elseif (choice == '2') {
if (size < 2) // There should be at least two elements
{
cout << " Invalid Input \n";
}
first = second = 0;
for (j = 0; j < size; j++) {
if (a[j] > first) // If current element is greater than first then update both first and second
{
second = first;
first = a[j];
}
if (a[j] > second && a[j] != first) // If arr[i] is in between first and second then update second
second = a[j];
}
if (second == 0)
cout << "There is no second largest element\n";
else
cout << "The second largest element is " << second << endl;
if (choice == '3') {
rvereseArr(a, 0, size - 1);
cout << "Reversed Array Elements are: \n";
printArray(a, size);
}
}
}
return 0;
}
Your choice == '3' is buried inside your choice == '2'
So yeah, never going to happen.
can you help me with something since you're so smart :)
i need to write a function to define a 2D array that both it's size and elements entered by the user..
i tried but idk what to do with the functions arguments
for example : readArray(int arr[][],int size) wont work to begin with.
can you help ?
arrays must have the size known at compile time in standard c++.
2-d is a great deal of trouble. It is far easier to work in 1-d and index it as if 2-d yourself. However you can do this...
vector< vector<type> > variable;
for example
vector < vector <int> > two_d_ints;
... //push back or allocate the space for the above, once done:
two_d_ints[row][col] = value;
it isn't my way, its how you do what you asked in c++.
void foo( vector<vector< type> > ¶matername); // <--- now its in a function.
It would be ideal if you can read up on vector and learn it. But give me a min and I will give you something that you can work with.
Its short and crude but its a starting point. You can do it without the copy with the vector's built in memory tools but I left that aside for now since you just saw vector for the first time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void make_2d(vector<vector<int> > &v)
{
int r, c;
cout << "enter # rows followed by num columns\n";
cin >> r >> c;
//construct it
vector<vector<int> > vec( r , vector<int> (c, 0));
//copy to input parameter.
v = vec;
}
int main()
{
vector<vector<int> > test;
make_2d(test); //put in 10x10
test[9][9] = 10;
cout << test[9][9];
}