arreglos

nova
14 de Abril del 2004
solo nesesito saber como declarar un arreglo para que el usuario pueda parametrizar el tamaño del arreglo

Oliverio
14 de Abril del 2004
Podria explicarte, pero lo mejor es que busques en google un tutorial sobre memoria dinamica y punteros :D

Suerte

noel solw
14 de Abril del 2004
Entiendo que un "arreglo" es lo que en ingles se denomina "array".
Te envio un programa que trabaja con un array dinamico:

// program d8_1.cpp - page 107.
// [a] : input the dimention of a dinamic array of integers and init it.
// [b] : print the array.
// [c] : get two integers a and b and delete members betwen a and b.
// [d] : print reversed array.
// [e] : duplicate array dimention and fill new members with a[i]%2.
// C exercices - daniel kilstein - 2000.
// written in borland c++ - ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>

int GetNum(char *msg)
{
int num;
cout << msg;
cin >> num;
return num;
} // GET NUM

void GetArrayValues(int *a,int dim)
{
for(int i = 0; i < dim; i++)
{
char s[10] = "a[", *p;
itoa(i,p,10);
strcat(s,p);
strcat(s,"] = ");
a[i] = GetNum(s);
}
cout << endl;
} // GET ARRAY VALUES

void ShowArray(int *a,int dim,char *msg,int sw = 0)
{
cout << msg;
if(sw)
for(int i = dim-1;i >= 0;i--)
cout << setw(4) << a[i];
else
for(int i = 0;i < dim;i++)
cout << setw(4) << a[i];
cout << endl << endl;
} // SHOW ARRAY

void swap(int *a,int *b)
{
int c = *a;
*a = *b;
*b = c;
} // SWAP

void ReductionProcess(int *a,int *dim)
{
int aa = GetNum(" first value : "),
bb = GetNum("second value : ");
cout << endl;
if(aa > bb)
swap(&aa,&bb);
for(int i = 0,k = 0;;)
{
for(;(a[i] >= aa && a[i] <= bb) && i < *dim-1;i++);
if(i == *dim-1)
{
a[k++] = a[i];
*dim = k;
return;
}
a[k++] = a[i++];
}
} // REDUCTION PROCESS

int *ExpansionProcess(int *a,int *dim)
{
int *temp = new int[(*dim)*2];
for(int i = 0,j = *dim;i < *dim;i++,j++)
{
temp[i] = a[i];
temp[j] = a[i]%2;
}
delete []a;
*dim = *dim*2;
return temp;
} // EXPANSION PROCESS

void main()
{
clrscr();
cout << "[a] : input the dimention of a dinamic array of integers"
"and init it.n"
"[b] : print the array.n"
"[c] : get two integers a and b and delete members betwen a and b."
"n[d] : print reversed array.n"
"[e] : duplicate array dimention and fill new members with a[i]%2.";
cout << "n-----------------------------------------------------"
"-------------------------n";
int dim = GetNum("array dimention : ");
cout << endl;
int *a = new int[dim];
GetArrayValues(a,dim);
ShowArray(a,dim," source array : ");
ReductionProcess(a,&dim);
ShowArray(a,dim," reduced array : ");
ShowArray(a,dim,"reversed print : ",1);
a = ExpansionProcess(a,&dim);
ShowArray(a,dim," final array : ");
cout << "-----------------------------------------------------"
<< "-------------------------n";
cout << "end of program - good bye ! ! ! n";
delete []a;
getch();
} // MAIN

/*
[a] : input the dimention of a dinamic array of integersand init it.
[b] : print the array.
[c] : get two integers a and b and delete members betwen a and b.
[d] : print reversed array.
[e] : duplicate array dimention and fill new members with a[i]%2.
------------------------------------------------------------------------------
array dimention : 10

a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8
a[8] = 9
a[9] = 10

source array : 1 2 3 4 5 6 7 8 9 10

first value : 4
second value : 7

reduced array : 1 2 3 8 9 10

reversed print : 10 9 8 3 2 1

final array : 1 2 3 8 9 10 1 0 1 0 1 0

------------------------------------------------------------------------------
end of program - good bye ! ! !
*/