0% found this document useful (0 votes)
16 views

Need To Know Algoritims

Uploaded by

nishchay naran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Need To Know Algoritims

Uploaded by

nishchay naran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Code to swap elements in an array

#include <iostream>

int main()
{
int Arr[] = {10,5,12,45,60,90};
int pos1 = -1;
int pos2 = -1;
for(int i = 0; i < 6; i++){
if(*(Arr+i)==10){
pos1 = i;
}
if(*(Arr +i) == 60){
pos2 = i;
}
}

int temp = *(Arr + pos1);


*(Arr + pos1) = *(Arr + pos2);
*(Arr + pos2) = temp;
for(int i = 0 ; i < 6; i++){
std::cout<<*(Arr +i)<<" ";
}
return 0;
}
Output:60 5 12 45 10 90

#include <iostream>
using namespace std;
int main()
{
int size;
cout<<"What is the array size";
cin>> size;
int Arr[size];

for(int i = 0; i < size; i++){


cout<<"Enter the value for index ["<<i<<"]";
cin>>Arr[i];
}
//element deletion
int pos;
cout<<endl<<"Which elemet do you want deleted";
cin >> pos;
for(int i = pos; i < size; i++){
if(i == size -1){
Arr[i] = 0;
}else{
Arr[i] = Arr[i+1];
}
}
int Arr2[size -1];

for(int i = 0; i < size -1; i++){


Arr2[i] = Arr[i];
cout<<Arr2[i]<<endl;
}
//use the new Arr2 for all further uses

return 0;
}

#include <iostream>
using namespace std;
int main()
{
int DArr[3][3];

for(int i = 0; i < 3; i++){


for(int j = 0; j < 3; j++){
int x;
cout<<"enter a value for index ["<<i<<"]["<<j<<"]: ";
cin>> x;

DArr[i][j] = x;
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout<<DArr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Output:

#include <iostream>
using namespace std;
int main()
{
int rows, collums;

cout<<"Enter the amount of rows then the amount of


collums"<<endl;

cin>>rows>>collums;

int** table = new int*[rows];


for(int i = 0; i < rows; i++){
table[i]= new int[collums];
}

//deallocation
for(int j = 0; j <rows; j++){
delete[]table[j];//must first deallocate all the arrays in
the array
}
delete []table;
table = NULL;
return 0;
}
#include <iostream>
using namespace std;

int main()
{
int rows, collums;
cout<<"Enter the amount of rows then the amount of
collums"<<endl;
cin>>rows>>collums;

int** table = new int*[rows];

for(int i = 0; i < rows; i++){


table[i]= new int[collums];
}

for(int i = 0; i < rows; i++){


for(int j= 0; j < collums; j++){
table[i][j] = 2;
}
}

for(int i = 0; i < rows; i++){


for(int j= 0; j < collums; j++){
cout<<table[i][j];
}
cout<<endl;
}

//deallocation
for(int j = 0; j <rows; j++){
delete[]table[j];//must first deallocate all the arrays in
the array
}
delete []table;
table = NULL;
return 0;
}

You might also like