Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Assighnment One
- /*Q:1 ===> Count the occurrence of each number in an array. Print the duplicate numbers based on the
- occurrence.*/
- #include<iostream>
- using namespace std;
- int main(){
- double myNumberArray[100] , myCountingArray[100];
- int size;
- cout << "Enter the size of array "; //declare the size of the array
- cin >> size;
- cout << endl << "Give the input" << endl; //take the input to fill the array
- for(int i = 0 ; i < size; i++){
- cin >> myNumberArray[i];//take the input
- myCountingArray[i] = -1 ;//initial value set for counting array
- } //store the array with value
- for(int i = 0 ; i < size ; i++){
- int count = 1;//initial count
- for(int j = i + 1 ; j < size ; j++){
- if(myNumberArray[i] == myNumberArray[j] ){
- count++; //increment the count
- myCountingArray[j] = 0 ; //if values are equal then fill that index in to 0 to avoid the repeat of counting
- }//check the two index are equal or not
- }
- if(myCountingArray[i] != 0 ){
- myCountingArray[i] = count;
- }//store the value counting
- } //main loop for counting the duplicate number
- for(int i = 0 ; i < size ; i++ ){
- if(myCountingArray[i] != 0 ){
- if( myCountingArray[i] > 1 ){
- cout << myNumberArray[i] << " occurred " << myCountingArray[i] << " times" << endl;
- } else if( myCountingArray[i] == 1 ) {
- cout << myNumberArray[i] << " occurred " << myCountingArray[i] << " time" << endl;
- }
- }
- } //print the counting of duplicate number occurrence.
- }
- /*Q:2 Find the largest value in a floating point array.*/
- #include <iostream>
- using namespace std ;
- int main(){
- double myValueArray[100] , myStoreValue;
- int size;
- cout << "Enter the size of array "; //declare the size of the array
- cin >> size;
- cout << endl << "Give the input" << endl; //take the input to fill the array
- for(int i = 0 ; i < size; i++){
- cin >> myValueArray[i];//take the input
- } //store the array with value
- for(int i = 0 ; i < size - 1 ; i++){
- for(int j = i+1 ; j < size ; j++ ){
- if( myValueArray[i] > myValueArray[j] ){
- myStoreValue = myValueArray[i] ;
- myValueArray[i] = myValueArray[j];
- myValueArray[j] = myStoreValue;
- } //swap the value
- } // check all the value with a value
- }
- cout << "Largest Number is: " << myValueArray[size - 1 ]; //print the largest value
- }
- /*Q:3 Sort an integer array in ascending order..*/
- #include <iostream>
- using namespace std ;
- int main(){
- double myValueArray[100] , myStoreValue;
- int size;
- cout << "Enter the size of array "; //declare the size of the array
- cin >> size;
- cout << endl << "Give the input" << endl; //take the input to fill the array
- for(int i = 0 ; i < size; i++){
- cin >> myValueArray[i];//take the input
- } //store the array with value
- for(int i = 0 ; i < size - 1 ; i++){
- for(int j = i+1 ; j < size ; j++ ){
- if( myValueArray[i] > myValueArray[j] ){
- myStoreValue = myValueArray[i] ;
- myValueArray[i] = myValueArray[j];
- myValueArray[j] = myStoreValue;
- } //swap the value
- } // check all the value with a value
- }
- cout << "Assecnding order ";
- for(int i = 0 ; i < size ; i++ ){
- cout << myValueArray[i] << " ";
- }//print the array in ascending order sort
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement