Change it to one array only

Hello everyone can someone edit my code i need my program to run only 1 array for insertion,deletion,sorting,searching.

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
 #include<iostream>
using namespace std;
void insertion(int n)	
{ 
	system("Color F6");
	int i, x, pos=0;
    int a[n];
		cout<<"Enter"<<n<<" numbers in ascending position: ";
    		for(i=0;i<n;++i)
        		cin>>a[i];
    			cout<<"\nEnter element to insert:";
    			cin>>x;
    
   			 for(i=0;i<n;++i)
       			 if(a[i]<=x&&x<a[i+1])
       			 {
            		pos=i+1;
           			 break;
       			 }
        
    		for(i=n+1;i>pos;--i)
       			 a[i]=a[i-1];
    				a[pos]=x;
    				cout<<"\n\nArray after inserting element:\n";
    
    		for(i=0;i<n+1;i++)
        		cout<<a[i]<<" ";
}
	void deletion(int n)
{
system("Color F6");
	int i, key,pos;
    int a[n];
		cout<<"Enter"<< n <<"numbers: ";
   			for(i=0;i<n;i++)
   			{
    			cin>>a[i];
   			}
   		cout<<"\n\nStored Data in array:  ";
   			for(i=0;i<n;i++)
   			{
    			cout<<a[i]<<" ";
   			}
   		cout<<"\n\nEnter poss. of element to delete: ";
   		cin>>pos;
  			if(pos>n)
  			 {
   				cout<<"\n\nThis value is out of range: ";
  			 }
  			else
   				{
   					--pos;
   					for(i=pos;i<=n-1;i++)
   					{
   						 a[i]=a[i+1];
  					 }
   		cout<<"\n\nNew data in array: ";
   			for(i=0;i<n-1;i++)
		   {
		    cout<<a[i]<<" ";
 			}
 				 }
}
	void searching(int n)
{
  system("Color F6");
    int a[n];
    int key,no,yes;
   cout<<"Enter"<<n<<"numbers: ";
    	for(int i = 0; i < n; i++){
        	cin>>a[i];
   		 }
    	cout<<"Enter the value to search: ";
    	cin>>key;
    	for(int i=0;i<n;i++){
        	if(key == a[i]){
           	 no = true;
            	cout<<"The value is found at index arr["<<i<<"]";
       	 		}
   	 		}
   	 if(!no){
     	   cout<<"Key not found!";
   	 } 
}
void sorting( int n)
{
  system("Color F6");
    int a[n];
    int i,j,hold;
cout<<"Enter "<<n <<"numbers: "<<endl;
		for(int i=0; i<n; i++)
		{
			cin>>a[i]; 
			} 
			cout<<endl; 
			cout<<"Orignally entered array by the user is: "<<endl;
		for(int j=0; j<n; j++)
		{
			cout<<a[j];
			cout<<endl; 
		} 
			cout<<endl;
		for(int i=0; i<n-1; i++)
			{
				for(int j=0; j<n-1; j++)
				{
					if(a[j]>a[j+1])
						{
							hold=a[j];
							a[j]=a[j+1];
							a[j+1]=hold; 
						}
				}	 
			} 
		cout<<"Sorted Array is: "<<endl;
			for(int i=0; i<n; i++)
			{
				cout<<a[i]<<endl; 
			}
}
int main()
{
	
  system("Color F6");
    int n,x,i,pos=0,cho,no, key , hold;
    
    cout<<"Menu"<<endl;
    cout<<"[1] Insertion"<<endl;
    cout<<"[2] Deletion"<<endl;
    cout<<"[3] serching"<<endl;
    cout<<"[4] sorting"<<endl;
    cout<<"Enter size of array: ";
    cin>>n;
    cout<<"Enter Number pick from the menu: ";
    cin>>cho;
    switch(cho)
    {
    	
	case 1:
    	insertion(n);
    break;
    
    case 2:
        deletion(n);
 	 break;
  case 3:
  	searching(n);
    break;
  		
	case 4:
		 sorting (n);
		break;
		
		default:
			cout<<"FAULT!!!";
		break;
    }
    return 0;
}
Cruicial,

You need to define your array in the main function. By defining the array in each function you are actually defining the array each time thus ending up with an array of garbage because it is not initialized nor has it been given any useful information to store.

Define the array in main and store the needed numbers there then pass the array to the functions either by value if nothing is to be changed or by reference if the values need to be changed or pass by reference and make it const if you do not want to change the values.

I believe this is your biggest problem to overcome first. If I find anything else when I test the code I will let you know.

Hope that helps,

Andy
Cruicial,

Sorry for any initial confusion, while I was thinking about other parts of your program I missed the fact that an array must be defined with a constant size e.g., a[5]; or a[]{ 1, 2, 3, 4, 5 };. This way the compiler will know how much space to set aside for the array. The other way to create an array during run time is to create the array dynamically after you get the size from the user. Another option is to create the array as a[100]{ 0 }; and use n to limit how much of the array to use. The initialization of zero will set every element of the array to zero, so if something were to go wrong you will get a zero and not garbage.

Another option is to use a vector whose size can be changed as the program runs. Also there are member functions for the vector class that make working with vectors easier than a conventional array e.g., sort.

When you call the functions pass the array as a parameter.

To the program: each function has code to place a number into the array. That section
1
2
3
4
5
6
cout << "Enter" << n << "numbers: ";
	
for (i = 0; i<n; i++)
{
	cin >> a[i];
}

should be put in its own function to build the array. Otherwise you are asking the user to provide the same numbers each time one of the functions is called.

In the delete function after I commented out the first for loop it appeared to work fine. Also the searching and sorting functions appear to work, but the insert function I think needs some work. It is hard to understand what to do.

Work on that and let us know if you have any other problems.

Hoe that helps,

Andy
now its great .
beside my comments in the program I found that the insertion function wanted to go behind the array but here is the full programe
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<iostream>
using namespace std;
int forall[100]={0};
void insertion(int n)	
{ 
	system("Color F6");
	int i, x, pos=0;
    
		cout<<"Enter"<<n<<" numbers in ascending position: ";
    		for(i=0;i<n;++i)
        		cin>>forall[i];
    			cout<<"\nEnter element to insert:";
    			cin>>x;
    
   			 for(i=0;i<n;++i)
       			 if(forall[i]<=x&&x<forall[i+1])
       			 {
            		pos=i+1;
           			 break;
       			 }
        
    		for(i=n-1;i>pos;--i)/////////////////////
       			 forall[i]=forall[i-1];//
    				forall[pos]=x;
    				cout<<"\n\nArray after inserting element:\n";
    
    		for(i=0;i<n;i++)/////////////////////i<n+1 deleted now it is i< n
        		cout<<forall[i]<<" ";
}
	void deletion(int n)
{
system("Color F6");
	int i, key,pos;
    
		cout<<"Enter"<< n <<"numbers: ";
   			for(i=0;i<n;i++)
   			{
    			cin>>forall[i];
   			}
   		cout<<"\n\nStored Data in array:  ";
   			for(i=0;i<n;i++)
   			{
    			cout<<forall[i]<<" ";
   			}
   		cout<<"\n\nEnter poss. of element to delete: ";
   		cin>>pos;
  			if(pos>n)
  			 {
   				cout<<"\n\nThis value is out of range: ";
  			 }
			else if(pos<1){
			cout<<"this value is less than 1"<<endl;
			}
  			else
   				{
   					--pos;
   					for(i=pos;i<=n-1;i++)
   					{
   						 forall[i]=forall[i+1];
  					 }
					 
   		cout<<"\n\nNew data in array: ";
   			for(i=0;i<n-1;i++)
		   {
		    cout<<forall[i]<<" ";
 			}
 				 }
}
	void searching(int n)
{
  system("Color F6");
    
    int key,no,yes;
   cout<<"Enter"<<n<<"numbers: ";
    	for(int i = 0; i < n; i++){
        	cin>>forall[i];
   		 }
    	cout<<"Enter the value to search: ";
    	cin>>key;
    	for(int i=0;i<n;i++){
        	if(key == forall[i]){
           	 no = true;
            	cout<<"The value is found at index arr["<<i<<"]";
       	 		}
   	 		}
   	 if(!no){
     	   cout<<"Key not found!";
   	 } 
}
void sorting( int n)
{
  system("Color F6");
    int i,j,hold;
cout<<"Enter "<<n <<"numbers: "<<endl;
		for(int i=0; i<n; i++)
		{
			cin>>forall[i]; 
			} 
			cout<<endl; 
			cout<<"Orignally entered array by the user is: "<<endl;
		for(int j=0; j<n; j++)
		{
			cout<<forall[j];
			cout<<endl;
		} 
			cout<<endl;
		for(int i=0; i<n-1; i++)
			{
				for(int j=i+1; j<n; j++)//j=i+1
				{
					if(forall[i]>forall[j]) //forall[i] not forall[j] and forall[j] not forall[j+1]
						{
							//some changing due to index changs
							hold=forall[j]; 
							forall[j]=forall[i];
							forall[i]=hold;
						}
				}	 
			} 
		cout<<"Sorted Array is: "<<endl;
			for(int i=0; i<n; i++)
			{
				cout<<forall[i]<<endl; 
			}
}
int main()
{
	
  system("Color F6");
    int n,x,i,pos=0,cho,no, key , hold;
	  
    cout<<"Menu"<<endl;
    cout<<"[1] Insertion"<<endl;
    cout<<"[2] Deletion"<<endl;
    cout<<"[3] serching"<<endl;
    cout<<"[4] sorting"<<endl;
    cout<<"Enter size of array: ";
    cin>>n;
    cout<<"Enter Number pick from the menu: ";
    cin>>cho;
    switch(cho)
    {
    	
	case 1:
    	insertion(n);
    break;
    
    case 2:
        deletion(n);
 	 break;
  case 3:
  	searching(n);
    break;
  		
	case 4:
		 sorting (n);
		break;
		
		default:
			cout<<"FAULT!!!";
		break;
    }

    return 0;
}

but I have used andy suggestion about the array
Topic archived. No new replies allowed.