functions not working

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 ?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace 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<<" ";
    }
   }
   else if(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;

}
It's an excellent example of why indentation MATTERS!
https://en.wikipedia.org/wiki/Indentation_style
Pick a style, and stick to it.

If you have an editor that auto-indents for you, use it.

If you don't have such an editor, then practice these skills.

All over the place indentation means you don't have a hope of seeing what goes where.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace 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 << " ";
      }
    } else if (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.
oh no, that's a beginner's mistake.. all good now thanks
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 ?
Last edited on
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;
Last edited on
@jonnin i appreciate the help but your way isn't familiar with me :(
i'm still a newbie .. it must be a function too ;(
it isn't my way, its how you do what you asked in c++.

void foo( vector<vector< type> > &paramatername); // <--- 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];
}

Last edited on
int x=1;
while (x == 1)
cout<<"thanks alot man"<<endl;
Topic archived. No new replies allowed.