Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- typedef struct{
- int tamanhoInicial;
- int qtdeElementos;
- int* vetorDinamico;
- int tamanhoVetor;
- } NossaLista;
- void printVetor(int *v, int tamanhoVetor){
- printf("\n (");
- for(int i = 0; i < tamanhoVetor; i++){
- printf(" %d ", v[i]);
- }
- printf(")\n");
- }
- void zerarVetor(int *v,int posInicial, int tamanhoVetor){
- for (int i = posInicial; i < tamanhoVetor; i++) {
- v[i]=0;
- }
- }
- int somaComponentes(int *v, int tamanhoVetor){
- int soma = 0;
- for (size_t i = 0; i < tamanhoVetor; i++) {
- soma += v[i];
- }
- return soma;
- }
- void atribuirValores(int *v,int tamanhoVetor){
- for (size_t i = 0; i < tamanhoVetor; i++){
- v[i] = (i*i+7)-(i%3)*2;
- }
- }
- int adicionaElemento(int *v, int i){
- printf("\n Digite o elemento: ");
- scanf("%d", &v[i]);
- }
- void aumentaVetor(int *v,int *tamanhoVetor){
- int i=0;
- printf("\n Digite por quanto você quer aumentar o vetor: ");
- scanf("%d", &i);
- int j = *tamanhoVetor;
- *tamanhoVetor += i;
- v = realloc(v,sizeof(int) * (*tamanhoVetor));
- zerarVetor(v, j,*tamanhoVetor);
- }
- void menu(NossaLista estrutura){
- int escolha, continuar = 1, pos = 0;
- while(continuar){
- printf("\n Digite a sua opção: \n 1 - Imprimir o vetor\n 2 - Adicionar Elemento\n 3 - Aumentar Vetor\n 4 - Terminar o programa\n > ");
- scanf("%d", &escolha);
- switch (escolha) {
- case 1:
- printVetor(estrutura.vetorDinamico,estrutura.tamanhoVetor);
- break;
- case 2:
- adicionaElemento(estrutura.vetorDinamico,estrutura.qtdeElementos);
- estrutura.qtdeElementos++;
- //pos = pos%tamanhoInicial;
- break;
- case 3:
- aumentaVetor(estrutura.vetorDinamico, &estrutura.tamanhoVetor);
- break;
- case 4:
- printf("\n O programa irá fechar!");
- continuar = 0;
- default:
- if (continuar!=0) printf("\n Opção inválida!!!\n");
- }
- }
- }
- int main(){
- setlocale(LC_CTYPE,"");
- NossaLista listaDinamica;
- printf(" Digite o tamanho inicial do vetor: ");
- scanf("%d", &listaDinamica.tamanhoInicial);
- listaDinamica.qtdeElementos = 0;
- listaDinamica.vetorDinamico = malloc(sizeof(int) * listaDinamica.tamanhoInicial);
- listaDinamica.tamanhoVetor = listaDinamica.tamanhoInicial;
- zerarVetor(listaDinamica.vetorDinamico, 0, listaDinamica.tamanhoVetor);
- menu(listaDinamica);
- printVetor(listaDinamica.vetorDinamico,listaDinamica.tamanhoVetor);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement