Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- void preencheInteiros(int* vetor, int tamanho){
- for (int i = 0; i < tamanho; i++)
- vetor[i] = i * i;
- }
- void exibirArray(int* vetor, int tamanho){
- cout << "Exibindo o array:" << endl;
- for (int i = 0; i < tamanho; i++){
- cout << vetor[i] << " ";
- }
- }
- int main(){
- int n;
- cout << "Digite o tamanho do array" << endl;
- cin >> n;
- int* meuArray;
- meuArray = (int*)malloc(sizeof(int)* n);
- preencheInteiros(meuArray, n);
- exibirArray(meuArray, n);
- int n2;
- cout << endl
- << "Qual deve ser o novo tamanho do array?" << endl;
- cin >> n;
- if (n > 0){
- int* novoArray;
- novoArray = (int *)realloc(meuArray, sizeof(int)* n);
- exibirArray(novoArray,n);
- if (novoArray == NULL){
- cout << endl << "Ocorreu um erro ao alocar o novo espaço!";
- }
- }
- else{
- cout << endl << "O numero do vetor deve ser maior que 0";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement