arrays help

so i need help trying to get the sequential search to where i need to have .
The function return the index where key is found or return -1 if not found and display a message if found or if not found

The numbers in the array are 80 32 20 16 10
=============
1: fill the array
2: print the array
3: calculate the average
4: show the highest number
5: double each number
6: selection sort in descending order
7: sequential search
8: show bar graph
9: quit
=============
Enter your choice:7

Enter the key:20

20 was found at index 2
but not sure how

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iomanip>
#include<iostream>

using namespace std;

const int SIZE=5;

void fillArray(int ar[]);
void printArray(const int ar[]);
double findAvgArray(const int ar[]);
int checkHighestArray(const int ar[]);
void doubleNumArray(int ar[],int s);
void sortArray(int ar[],int s);
bool sequeSearch(const int ar[],int s,int k);
void showBarGraph(int ar[]);
int main()
{
  // const int SIZE=5;
  int ar[SIZE];
  //fillArray[ar];
  int choice;
  int k;
  do{
    cout<<"==================="<<endl;
    cout<<"1:Fill the array"<<endl;
    cout<<"2:Print the array"<<endl;
    cout<<"3:Calculate the average"<<endl;
    cout<<"4:Show the highest Number"<<endl;
    cout<<"5:Double each number"<<endl;
    cout<<"6:sort in descending order"<<endl;
    cout<<"7:Sequential Search"<<endl;
    cout<<"8:Show Bar Graph"<<endl;
    cout<<"9:Quit"<<endl;
    cout<<"==================="<<endl;
    cout<<"Enter your choice: ";
    cin>>choice;

    switch(choice)
      {
 case 1: fillArray(ar);
        break;
      case 2: printArray(ar);
        break;
      case 3:findAvgArray(ar);
        break;
      case 4:cout<<"Highest number is "<<checkHighestArray(ar);
        break;
      case 5:doubleNumArray((ar),SIZE);
        break;
      case 6:sortArray((ar),SIZE);
        break;
      case 7:cout<<"Enter the Key: ";
        cin>>k;
        sequeSearch((ar),SIZE,k);
        break;
      case 8: showBarGraph(ar);
        break;
      case 9: cout<<"Thank you for using our System."<<endl;
        break;
      default:cout<<"Invalid choice"<<endl;
 break;
      }
  }while(choice!=9);

  // fillArray(ar);
  //printArray(ar);
  return 0;
}
void fillArray(int ar[])
  {
    for(int i=0;i<SIZE;i++)
      {
        cout<<"Enter a number ";
        cin>>ar[i];
      }
    cout<<endl;
  }

  void printArray(const int ar[])
  {
    cout<<"The numbers in the array are ";

    for(int i=0;i<SIZE;i++)
      cout<<ar[i]<<" ";

    cout<<endl;
  }
double findAvgArray(const int ar[])
{
  cout<<"The average number is ";
  double total=0;
  for(int i=0;i<SIZE;i++)
    total +=ar[i];
  total=total/SIZE;
  cout<<total<<endl;
  return total;
}
int checkHighestArray(const int ar[])
{
  int highestNumber=0;
 for(int i=1;i<SIZE;i++)
    if(ar[i]>ar[highestNumber])
      highestNumber=i;

    return ar[highestNumber];

  //cout<<"The highest number is "<<highestNumber<<endl;
  //return ar[highestNumber];
}
void doubleNumArray(int ar[],int s)
{
  int temp;
  //cout<<"The numbers in the array doubled are ";
  for(int i=0;i<SIZE;i++)
  temp=ar[i]*2;
  cout<<endl;

}
void sortArray(int ar[],int s)
{
 int smallIndx;
  int temp;

  for(int last=SIZE-1; last>=1; last--)
    {
      smallIndx =0;
      for(int i= 1; i<=last; i++)
        {
          if(ar[i] < ar[smallIndx])
            smallIndx= i;
        }
      temp=ar[smallIndx];
      ar[smallIndx]= ar[last];
      ar[last]=temp;
    }
  // cout<<"All entries in the array have been sorted."<<endl;
  cout<<endl;
 }

bool sequeSearch(const int ar[],int s, int k)
{
  int temp=0;
  for(int i=0;i<s;i++)
    {
      if(ar[i]==k)
        {
          temp=1;
          break;
        }
    }
}

void showBarGraph(int ar[])
{
  for (int c=0;c<SIZE;c++)
    {
      cout<<"Slot "<<c+1<<" : ";
      for(int f=0;f<ar[c];f++)
        {
          cout<<"*";
        }
      cout<<endl;
    }
}
   

as well need help with bar graph to display like this mine isnt correct anything helps thank you
The numbers in the array are 20 16 32 80 10
=============
1: fill the array
2: print the array
3: calculate the average
4: show the highest number
5: double each
number
6: selection sort in descending order
7: sequential search
8: show bar graph
9: quit
=============
Enter your choice:
8
Slot 0: **
Slot 1: *
Slot 2: ***
Slot 3: ********
Slot 4: *
Last edited on
Your code works fine with 20, 16, 32, 80, 10, as long as you choose 1 to fill the array first before you choose 8 to show bar graph.
it doesnt display how i want it when i run it shows
The numbers in the array are 20 16 32 80 10
===================
1:Fill the array
2:Print the array
3:Calculate the average
4:Show the highest Number
5:Double each number
6:sort in descending order
7:Sequential Search
8:Show Bar Graph
9:Quit
===================
Enter your choice: 8
Slot 1 : ********************
Slot 2 : ****************
Slot 3 : ********************************
Slot 4 : ********************************************************************************
Slot 5 : **********

this is how it should be

Slot 1 : **
Slot 2 : *
Slot 3 : ***
Slot 4 : ********
Slot 5 : *
===================
so it displays the first number only
Last edited on
Here is my full output.
===================
1:Fill the array
2:Print the array
3:Calculate the average
4:Show the highest Number
5:Double each number
6:sort in descending order
7:Sequential Search
8:Show Bar Graph
9:Quit
===================
Enter your choice: 1
Enter a number 20
Enter a number 16
Enter a number 32
Enter a number 80
Enter a number 10

===================
1:Fill the array
2:Print the array
3:Calculate the average
4:Show the highest Number
5:Double each number
6:sort in descending order
7:Sequential Search
8:Show Bar Graph
9:Quit
===================
Enter your choice: 8
Slot 1 : ********************
Slot 2 : ****************
Slot 3 : ********************************
Slot 4 : ********************************************************************************
Slot 5 : **********
===================

What could be wrong with it?
so here is an example of what it should display
The numbers in the array are 10 8 16 40 5
=============
1: fill the array
2: print the array
3: calculate the average
4: show the highest number
5: double each number
6: selection sort in descending order
7: sequential search
8: show bar
graph
9: quit
=============
Enter your choice:
8
Slot 0: *
Slot 1:
Slot 2: *
Slot 3: ****
Slot 4:
so it displays the first number only
I understand it now.
You can use this function :
1
2
3
4
5
6
7
8
9
void showBarGraph(int ar[])
{
	int i, j, k;
	for(i = 0; i < SIZE; i++)
	{
		k = ar[i] / 10;
		for(j = 0; j < k; j++) cout << '*'; cout << endl;
	}
}
do you get how to get the sequential search for case 7??
Last edited on
Well, something like this should suffice :

bool sequeSearch(const int ar[],int s, int k)
{
  int temp=0;
  for(int i=0;i<s;i++)
    {
      if(ar[i]==k)
        {
          temp=1;
          break;
        }
    }
    return (temp == 1);
}
my question is how can i display the message in main
Enter your choice:7

Enter the key:20

20 was found at index 2 <===display this and as well display when the number entered wasnt correct
You can use this fix :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool sequeSearch(const int ar[], int s, int k)
{
  int temp=0;
  for(int i=0;i<s;i++)
    {
      if(ar[i]==k)
        {
          temp=1;
          break;
        }
    }

    if(temp != 0)
	{
		cout << k << " was found at index " << i + 1 << endl;
	}
    return (temp == 1);
}
is their a way i could be able to display that message in main though ?? instead of inside the bool sequeSearch function.
You can use it but I am not sure.
1
2
3
4
5
6
7
case 7 : 
        int s_i;
        cout<<"Enter the Key: ";
        cin>>k;
        if(sequeSearch((ar), SIZE, s_i, k))
        cout << k << " was found at index " << s_i + 1 << endl;
        break;


And the function :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool sequeSearch(const int ar[], int s, int &s_i, int k)
{
  int temp=0;
  for(int i=0;i<s;i++)
    {
      if(ar[i]==k)
        {
          temp=1;
          break;
        }
    }

    if(temp != 0)
	{
		s_i = i;
	}
    return (temp == 1);
}


Please change the function prototype also.
bool sequeSearch(const int ar[] , int s , int &s_i, int k);
it doesnt work it gives me an error in line 15
Topic archived. No new replies allowed.