---
# System prepended metadata

title: Chapter 5(Arrays)
tags: [C++ Lab]

---

###### tags: `C++ Lab`
# Chapter 5(Arrays)
1. Print sorted array (dsec)
```#include <iostream>
using namespace std;

int main(){
	int n,t,a[100],sum=0;
	cout<<"Input n: ";
	cin>>n;
	
	for(int i=0;i<n;i++) {
		cout<<"Input element #"<<i+1<<": ";
		cin>>a[i];
	}
	
//sort array(desc)
	for(int i=0;i<n;i++) {
		for(int j=i+1;j<n;j++) {
			if(a[j]>a[i]) {
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}	
	}
	
//	print sorted
	cout<<"Sorted: "<<endl;
	for(int i=0;i<n;i++) {
		cout<<a[i]<<" ";
	}
	cout<<endl;
return 0;
}
```
2. Print sorted array (asc) and remove duplicates
```#include <iostream>
using namespace std;

int main(){
	int n,t,a[100];
	cout<<"Input n: ";
	cin>>n;
	
	for(int i=0;i<n;i++) {
		cout<<"Input element #"<<i+1<<": ";
		cin>>a[i];
	}

//sort array(asc)
	for(int i=0;i<n;i++) {
		for(int j=i+1;j<n;j++) {
			if(a[j]<a[i]) {
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}	
	}
	
//remove duplicates
		for(int i=0;i<n;i++) {
		for(int j=i+1;j<n;j++) {
			if(a[i]==a[j]) {
				for(int k=j;k<n;k++) {
					a[k]=a[k+1];
				}
				n--;
				j--;
			}
		}	
	}
	
	cout<<"Sorted and Deleted Duplicates: "<<endl;
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
return 0;
}
```
3. Find largest,smallest and sum of elements in the array 
```#include <iostream>
using namespace std;

int main(){
	int n,t,a[100],sum=0;
	cout<<"Input n: ";
	cin>>n;
	
	for(int i=0;i<n;i++) {
		cout<<"Input element #"<<i+1<<": ";
		cin>>a[i];
	}
	
//sort array(asc)
	for(int i=0;i<n;i++) {
		for(int j=i+1;j<n;j++) {
			if(a[j]<a[i]) {
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}	
	}
		
//sum array
	for(int i=0;i<n;i++) {
		sum+=a[i];
	}
	
	cout<<"largest: "<<a[n-1]<<endl;
	cout<<"smallest: "<<a[0]<<endl;
	cout<<"sum: "<<sum<<endl;
return 0;
}
```
4. Create increasing order array, insert a number into array(need to keep the order)
```#include <iostream>
using namespace std;


int main(){
    int n,a[100];
    cout<<"Input n: ";
    cin>>n;
    
	a[0]=0;

    for(int i=1;i<n;i++) {
        cout<<"Input element #"<<i<<": ";
        cin>>a[i];

        if(a[i]<a[i-1]) {
            cout<<"The input is incorrect"<<endl;
            cout<<"Input element #"<<i<<": ";
            cin>>a[i];
        } else {
           continue;
        }
    }
	
	cout<<"Array: "<<endl;
    for(int i=1;i<n;i++) {
        cout<<a[i]<<" ";
	}
	cout<<endl;
	
	int x;
	cout<<"Input x: ";
	cin>>x;
	
	int p=0;
	while(a[p]<x) p++;
	
	for(int i=n;i>=p;i--) {
		a[i]=a[i-1];
	}
	
	n++;
	a[p]=x;
	
    cout<<"Sorted: "<<endl;
    for(int i=1;i<n;i++)
        cout<<a[i]<<" ";
return 0;
}
```
5. Create 2-dimensional array. Input number to find(location, how many matches)
```#include <iostream>
#include <string>
using namespace std;


int main(){
	int n,m,i,j,k=0;
	cout<<"columns: ";
	cin>>m;
	cout<<"rows: ";
	cin>>n;
	string num[n][m];	
	
	//input 2-dimensional array
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			cout<<"["<<i<<"]["<<j<<"]:  ";
	  		cin>>num[i][j];
		}
	}
	
	string x;
	cout<<"Enter another integer: ";
	cin>>x;
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			if(num[i][j]==x) {
				cout<<"location is: "<<"["<<i<<"]["<<j<<"]"<<endl;
				k++;
				
			}
		}
	}
	cout<<"The value of "<<x<<" appeared "<<k<<" time(s)."<<endl;
return 0;
}
```
6. convert the position (row>col, col>row)
```#include <iostream>
#include <string>
using namespace std;


int main(){
	int i,j;
	string num[4][4],trans[4][4];
	
	//input 2-dimensional array
	for(i=0;i<4;i++) {
		for(j=0;j<4;j++) {
			cout<<"["<<i<<"]["<<j<<"]:  ";
	  		cin>>num[i][j];
		}
	}
	
	//transposition	
	for (i=0;i<4;i++) {
  		for (j=0;j<4;j++) {
   	 		trans[j][i] = num[i][j];
	 	} 
	}
	
	//output original 2-dimensional array
	cout<<"original:"<<endl;
    	for(i=0;i<4;i++) {
	  		for(j=0;j<4;j++) {
	  			cout<<" "<<num[i][j];
			}
		cout<<endl;
		}
		
	//output changed 2-dimensional array
	cout<<"transposition:"<<endl;
    	for(i=0;i<4;i++) {
	  		for(j=0;j<4;j++) {
	  			cout<<" "<<trans[i][j];
			}
		cout<<endl;
		}		 
return 0;		
}
```
7. Find odd, even, prime and non-prime numbers in arrays
```#include <iostream>
using namespace std;

int main(){
	int i=0,k=0,n,j;
	cout<<"How many numbers you want to input: ";
	cin>>n;
	
	int arr[n];
	for(j=0;j<n;j++) {
	  		cin>>arr[j];
	}
	
	cout<<"Lists of odd numbers: ";
	for(j=0;j<n;j++) {
		if((arr[j]%2)!=0) {
			cout<<arr[j]<<" ";
			i++;
		}
	}
	cout<<"Total: "<<i<<endl;
	
	cout<<"Lists of even numbers: ";
	for(j=0;j<n;j++) {
		if((arr[j]%2)==0) {
			cout<<arr[j]<<" ";
			k++;
		}
	}
	cout<<"Total: "<<k<<endl;
	
	cout<<"Lists of prime numbers: ";
	for(j=0;j<n;j++) {
	int flag=0;	
	int end=arr[j]/2;
		for(int i=2;i<=end;i++) {
            if(arr[j]%i==0) {
                flag=1;
				break;
            }
        }
		if(flag==1||arr[j]==1) {
			continue;	
		} else {
			cout<<arr[j]<<" ";
		}
	}
	cout<<endl;
	
	cout<<"Lists of not prime numbers: ";
	for(j=0;j<n;j++) {
	int flag=0;	
	int end=arr[j]/2;
		for(int i=2;i<=end;i++) {
            if(arr[j]%i==0||arr[j]==1) {
                flag=1;
				break;
            }
        }
		if(flag==1||arr[j]==1) {
			cout<<arr[j]<<" ";	
		} else {
			continue;
		}
	}
	cout<<endl;	
return 0;
}
```
8. Calculate the sum of squares of negative numbers in arrays
```#include <iostream>
using namespace std;

int main(){
	int n,sum=0;
	cout<<"How many numbers you want to input: ";
	cin>>n;
	
	int arr[n];
	for(int j=0;j<n;j++) {
	  	cin>>arr[j];
	if(arr[j]<0) {
			sum+=arr[j]*arr[j];
		}	
	}
		
	cout<<"The sum of squares of negative numbers: "<<sum<<endl;
return 0;
}
```
9. reverse 1-dimensional array
```#include <iostream>
#include <string>
using namespace std;

int main(){
	int i,n,j;
	cout<<"How many numbers you want to input: ";
	cin>>n;
	string num[n],num1[n];

	for(i=0;i<n;i++) {
	  		cin>>num[i];
		}

	cout<<"reverse: "<<endl;
    for(int i=n-1; i>=0; i--)
        num1[j++]=num[i];


	for(int i=0;i<n;i++)
		cout<<num1[i]<<" ";

return 0;
}
```
10. Calculate the sum of 2 matrix
```
example: 3 8 + 4  0 = 7  8 
         4 6   1 -9   5 -3
```
```#include <iostream>
using namespace std;


int main(){
	int n,m,i,j;
	cout<<"rows: ";
	cin>>n;
	cout<<"columns: ";
	cin>>m;

	int a[n][m],b[n][m],sum[n][m];	
	
	cout<<"Matrix A: "<<endl;
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			cout<<"["<<i<<"]["<<j<<"]:  ";
	  		cin>>a[i][j];
		}
	}
	
	cout<<"Matrix B: "<<endl;
	for(i=0;i<n;i++) {
		for(j=0;j<m;j++) {
			cout<<"["<<i<<"]["<<j<<"]:  ";
	  		cin>>b[i][j];
		}
	}

	for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            sum[i][j]=a[i][j]+b[i][j];
		}
	}
	
	cout<<"Sum:"<<endl;
    	for(i=0;i<n;i++) {
	  		for(j=0;j<m;j++) {
	  			cout<<" "<<sum[i][j];
			}
		cout<<endl;
		}
	return 0;	
}
```
11. The product of 2 matrix
```
example: 3 4 2 * 13 9 7 15 = 83 63 37 75 
		  8 7 4 6
		  6 4 0 3 
```
```#include <iostream>
using namespace std;

int main(){
    int x,n;
    cout<<"Matrix A length: ";
    cin>>x;
    cout<<"Matrix B column: ";
    cin>>n;

    int a[x],b[x][n],mul[n];


    cout<<"Matrix A: "<<endl;
    for(int i=0;i<x;i++) {
        cout<<"["<<i<<"]:  ";
        cin>>a[i];
    }

    cout<<"Matrix B: "<<endl;
    for(int i=0;i<x;i++) {
        for(int j=0;j<n;j++) {
            cout<<"["<<i<<"]["<<j<<"]:  ";
            cin>>b[i][j];
        }
    }

    for(int i=0;i<n;i++) {
        mul[i]=0;
    }

    int p=0;
        for(int j=0;j<n;j++){
            for(int k=0;k<x;k++){
                mul[j]+=a[p]*b[k][j];
                p++;
            }
            p=0;
        }

    cout<<"Ans: "<<endl;
    for(int i=0;i<n;i++) {
        cout<<mul[i]<<" ";
    }
return 0;
}
```
12. merge 2 ordered arrays(not using merged and then sorted)
```#include <iostream>
using namespace std;

int main(){
    int x,n;
    cout<<"Matrix A length: ";
    cin>>x;
    cout<<"Matrix B column: ";
    cin>>n;

    int a[x],b[x][n],mul[n];


    cout<<"Matrix A: "<<endl;
    for(int i=0;i<x;i++) {
        cout<<"["<<i<<"]:  ";
        cin>>a[i];
    }

    cout<<"Matrix B: "<<endl;
    for(int i=0;i<x;i++) {
        for(int j=0;j<n;j++) {
            cout<<"["<<i<<"]["<<j<<"]:  ";
            cin>>b[i][j];
        }
    }

    for(int i=0;i<n;i++) {
        mul[i]=0;
    }

    int p=0;
        for(int j=0;j<n;j++){
            for(int k=0;k<x;k++){
                mul[j]+=a[p]*b[k][j];
                p++;
            }
            p=0;
        }

    cout<<"Ans: "<<endl;
    for(int i=0;i<n;i++) {
        cout<<mul[i]<<" ";
    }
return 0;
}
```

