-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.cpp
More file actions
137 lines (128 loc) · 3.45 KB
/
array.cpp
File metadata and controls
137 lines (128 loc) · 3.45 KB
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
// here we discussion the important data structure that is ARRAY
#include<iostream>
using namespace std;
int main(){
int marks[5]={100,2,322,212,121};
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
marks[4]=99;
cout<<marks[4]<<endl;
cout<<marks[5]<<endl;
}
// smallest number in the array
#include<iostream>
using namespace std;
int main(){
int array[5]={4,5,2,1,8};
int size=5;
int smallest=INT8_MAX;
int largest=INT8_MIN;
for(int i=0;i<=size;i++){
if(array[i]>largest){
largest=array[i];
}
else if(array[i]<smallest){
smallest=array[i];
}
}
cout<<"smallest number is "<<smallest<<endl;
cout<<"Largest number is "<<largest<<endl;
}
/pass by reference
Two of the common ones are Passing by Value and Passing by Reference. Passing by reference allows a function to modify a variable without creating a copy. We have to declare reference variables.
The memory location of the passed variable and parameter is the same. Therefore, any change to the parameter also reflects in the variable inside its parent function.
This article focuses on discussing how to pass variables by reference in C++.
/ Reverse of an Array
#include<iostream>
using namespace std;
int Reverse(int array[],int size){
int start=0;
int end=size-1;
while(start<end){
swap(array[start],array[end]);
start++;
end--;
};
}
int main(){
int arrays[]={9,8,7,6,5,4,3,2,1};
int sz=9;
int result=Reverse(arrays,sz);
for(int i=0;i<sz;i++){
cout<<arrays[i]<<"";
}
cout<<endl;
return 0;
}
/Calculate the suum of the and product of the number present in the array
#include<iostream>
using namespace std;
void Sum(int array[],int size){
int sum=0;
for(int i=0;i<size;i++){
sum+=array[i];
}
cout<<sum<<endl;
}
void Product(int array[],int size){
int product=1;
for(int i=0;i<size;i++){
product*=array[i];
}
cout<<product<<endl;
}
int main(){
int arrays[]={1,2,3,4,5,6};
int size=6;
Sum(arrays,size);
Product(arrays,size);
}
/ Write a function to swap the max and min value of the numbers in the arrays
#include<iostream>
using namespace std;
int main(){
int array[]={2,33,45,23,2321,121};
int size=6;
int max_num=0;
int min=0;
for(int i=0;i<size;i++){
if(array[i]>array[max_num]){
max_num=i;
}
else if(array[i]<array[min]){
min=i;
};
};
int temp=array[max_num];
array[max_num]=array[min];
array[min]=temp;
cout << "Array after swapping max and min: ";
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
/ Question:to find the unique number in the arrays
#include<iostream>
using namespace std;
void Unique_number(int array[],int size){
for(int i=0;i<size;i++){
bool isuniqque=true;
for(int j=0;j<size;j++){
if(i!=j && array[i]!=array[j]){
isuniqque=false;
break;
}}
if (isUnique) {
cout << arr[i] << " ";
}
}
}
}
int main(){
int array[]={1,3,4,2,1,5,6,3}
int size=8;
Unique_number(array,size)
}