Please Help Me

Pages: 12
Below is an example on the use of linear search in the array.

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
// This program demonstrates the searchList function, which
// performs a linear search on an integer array.
#include <iostream>
using namespace std;

// Function prototype
int searchList(const int [], int, int);
const int SIZE = 5;

int main()
{
   int tests[SIZE] = {87, 75, 98, 100, 82};
   int results;

   // Search the array for 100.
   results = searchList(tests, SIZE, 100);
   
   // If searchList returned -1, then 100 was not found.
   if (results == -1)
      cout << "You did not earn 100 points on any test\n";
   else
   {
      // Otherwise results contains the subscript of
      // the first 100 in the array.
      cout << "You earned 100 points on test ";
      cout << (results + 1) << endl;
   }
   return 0;
}

//*****************************************************************
// The searchList function performs a linear search on an         *
// integer array. The array list, which has a maximum of numElems *
// elements, is searched for the number stored in value. If the   *
// number is found, its array subscript is returned. Otherwise,   *
// -1 is returned indicating the value was not in the array.      *
//*****************************************************************

int searchList(const int list[], int numElems, int value)
{
   int index = 0;       // Used as a subscript to search array
   int position = -1;   // To record position of search value
   bool found = false;  // Flag to indicate if the value was found

   while (index < numElems && !found)
   {
      if (list[index] == value)  // If the value is found
      {
         found = true;           // Set the flag
         position = index;       // Record the value's subscript
      }
      index++;                   // Go to the next element
   }
   return position;              // Return the position, or -1
}
search is ok.. but last thing;

user set max student to "10"
then go to menu
user select "1 - new student"
after 1 student enter program should go back to menu
if user want to enter 10 student should go back menu 10 times
and program put max size "10"

is it possible?
look my last codes

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
#include <iostream>
#include <stdio.h>
using namespace std;

int main ()
 {
int *ogrencino, *ogrencinot;
int kontrol=0,i,ara,toplam,ono, secim,onot,exit=1;
cout <<"Öğrenci Sayısını Giriniz: ";
cin>>toplam;
ogrencino= new int[toplam];
ogrencinot= new int[toplam];
while (exit == 1)
{
cout <<"\n1 - Not Ver \n2 - Not Gör \n3 - Tum Ogrencilerin Notlari\n4 - Cikis\n Seciminiz : ";
cin>>secim;
switch(secim)
{
case 1:
for (i=0;i<toplam;i++)
{
cout <<"Ogrenci No :";
cin >> ogrencino[i];
cout << "Ogrenci Not :";
cin >> ogrencinot[i];
}

break;
case 2:
cout<<"Ogrenci No :";
cin>>ara;


for (i=0;i<toplam;i++)
{
if(ogrencino[i]==ara)
{
kontrol=1;
cout<<ogrencinot[i];
}
}
if(kontrol==0)
{
cout<<"bulunamadı";
}



break;
case 3:
for (i=0;i<toplam;i++)
{
cout<<"\n   "<<ogrencino[i] <<"-"<<ogrencinot[i] ;
//cout<<"\nOgrenci Not:"<<onot;
}
break;
case 4:

exit =0;
}
}
//return 0;
}


I would put a while loop under cin>>toplam;

For instance:

1
2
3
4
5
6
7
8
	cout << "Öğrenci Sayısını Giriniz: ";
	cin >> toplam;
	while (toplam > 10) //while user select number higher than 10.
	{
		cout << "You exceed it the max number (10)" << endl;
		cout << "Öğrenci Sayısını Giriniz: ";
		cin >> toplam;
	}

Last edited on
hello can i ask u new problem?
i need convert that codes to 2d array for new program.
when i press 1 program ll go new student
in that case program must do new menu for exams.. 1 - first exam 2- second exam 3- final 4- total

i tried ogrencinot = new int [total] [3] but it doesn't work exatcly
2d array -> **
Topic archived. No new replies allowed.
Pages: 12