Arrays are set of variables that can hold multiple variables.
How do we declare Arrays:
string arr[3];
Here, the array type is string and name is "arr", and it will hold 3 elements, meaning the size of the array.
The size can be omitted by doing this : string arr[]; If the size of the array determined to unknown. But if it is known, it's better to declare the size.
How do we initialize Arrays:
< under -progress > This post is under progress, if you are a learner and want to contribute your progress in this blog, contact me! ashik.shovon03@gmail.com < under -progress >
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 | #include <iostream> using namespace std; void printArray(int array[],int size); void printEvenArray(int array[],int size); void printEvenArray2(int array[],int size); void printOddArray(int array[],int size); int sumOfArray(int array[],int size); double avgOfArray(int array[],int size) ; int searchLinear(int arr[],int size,int search); int findMinimum(int arr[],int size); int main() { char arr[]={'a','b'}; cout << arr <<endl; int const size =5; int arr2[size]={0,2,4,5}; int arr3[6]={50,22,74,51,62,102}; printArray(arr2,size); //0 | 2 | 4 | 5 | 0 cout << "\n"; printEvenArray(arr2,size); //0 | 4 | 0 | cout << "\n"; printEvenArray2(arr2,size); //0 | 4 | 0 | cout << "\n"; printOddArray(arr2,size); //2 | 5 | cout << "\n"; cout<< sumOfArray(arr2,size); //11 cout << "\n"; cout<< avgOfArray(arr2,size); //2.2 cout << "\n"; cout<< searchLinear(arr2,size,5);//3 cout << "\n"; cout<< searchLinear(arr2,size,4);//2 cout << "\n"; cout<< findMinimum(arr3,6); //22 cout << "\n"; cout<< findMinimum(arr2,size); //0 cout << "\n"; return 0; } void printArray(int array[],int size) // to print the whole array { for (int i=0; i<size; i++) //the classic iterator. if (i==size-1) // if i reaches the last element cout <<array[i]<<" "; //so that the last " | " doesn't show up else cout <<array[i]<<" | "; // so that " | " shows up untill it reaches the last element } void printEvenArray(int array[],int size) // to print the even indexes in the array (for loop) { for (int i=0; i<size; i = i+2) cout <<array[i]<<" | "; } void printEvenArray2(int array[],int size) // to print the even indexes in the array (do + if) { int i=0; do { if (i%2==0){ cout <<array[i]<<" | "; } i = i+1; }while (i<size); } void printOddArray(int array[],int size) // to print the odd indexes in the array { for (int i=1; i<size; i = i+2) cout <<array[i]<<" | "; } int sumOfArray(int array[],int size) // to return sum of the elements of an array { int sum=0; for (int i=0; i<size; i = i+1) { sum = sum+array[i]; } return sum; } double avgOfArray(int array[],int size) // to return average of the elements of an array { /* int sum=0; for (int i=0; i<size; i = i+1) { sum = sum+array[i]; } return sum/size; */ // all those things can be simplified into this: return (sumOfArray(array,size)*1.0)/size; //multiplying by 1.0 to get a double value } int searchLinear(int arr[],int size,int search) //iterates through the elements and searchs for an element { for (int i=0; i<size; i = i+1) { if (arr[i]==search) { return i; // if it exists in any index the function returns the index } } return -1; //// if it doesn't exist, the function returns -1 } int findMinimum(int arr[],int size) //iterates through the elements and searchs for a minimum value { int min=arr[0]; //assumes that the first one is the minimum for (int i=1; i<size;i++) { if (arr[i]<min) // iterate an compares with min variable min = arr[i]; // lowest value gets to enter into min variable } return min; // lowest value is returned } |
0 comments:
Post a Comment