Taller 4 - Apuntadores I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <iostream>

using namespace std;

void random(int integers[], int size){


for (int i = 0; i < size; i++){
integers[i] = rand()%100;
}
}

void printNumbers(int integers[], int size){


cout << "\n";
for (int i = 0; i < size; i++){
cout << i + 1 << ". " << integers[i] << endl;
}
}

void printAddress(int integers[], int size){


cout << "\n";
for (int i = 0; i < size; i++){
cout << i + 1 << ". " << &integers[i] << endl;
}
}

void arrayAddress(int integers[], int* addresses[], int size){


for (int i = 0; i < size; i++){
addresses[i] = &integers[i];
}
}

void printArrayAdd(int* addresses[], int size){


cout << "\n";
for (int i = 0; i < size; i++){
cout << i + 1 << ". " << addresses[i] << endl;
}
}

void printIntAdd(int* addresses[], int size){


cout << "\n";
for (int i = 0; i < size; i++){
cout << i + 1 << ". " << *addresses[i] << endl;
}
}

void sortAdd(int integers[], int* addresses[], int size){


int* auxAdd;
int pos, auxInt[size], auxInt2;

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


auxInt[i] = integers[i];
}

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


for (int j = 0; j < size; j++){
if (auxInt[j] > auxInt[j + 1]){
auxInt2 = auxInt[j];
auxInt[j] = auxInt[j + 1];
auxInt[j + 1] = auxInt2;

auxAdd = addresses[j];
addresses[j] = addresses[j + 1];
addresses[j + 1] = auxAdd;
}
}
}
}

int main() {

//int size;
int size = 10, integers[size], option;
int* addresses[size];

/*cout << "What is the size of array?" << endl;


cin >> size;*/

while (option != -1){


cout << "\nChoose an option" << endl;
cout << "\nOption 1: generate an array with random numbers" << endl;
cout << "Option 2: print numbers" << endl;
cout << "Option 3: print addresses of numbers" << endl;
cout << "Option 4: generate an array with the addresses" << endl;
cout << "Option 5: print array with addresses" << endl;
cout << "Option 6: print numbers from their addresses" << endl;
cout << "Option 7: sort array of addressses" << endl;
cout << "Option 8: print numbers with their sorted addresses" <<
endl;
cout << "Insert -1 if you want to exit the code" << endl;
cout << "\nOption: ";
cin >> option;

switch (option){
case 1:{
random(integers, size);
break;
}
case 2:{
printNumbers(integers, size);
break;
}
case 3:{
printAddress(integers, size);
break;
}
case 4:{
arrayAddress(integers, addresses, size);
break;
}
case 5:{
printArrayAdd(addresses, size);
break;
}
case 6:{
printIntAdd(addresses, size);
break;
}
case 7:{
sortAdd(integers, addresses, size);
break;
}
case 8:{
printIntAdd(addresses, size);
break;
}
}
}
}

You might also like