Reputation: 219
Im trying to implement arrays in order for my program to save any amount of data, an equivalent to C# for this
int nT, i;
cTrabajador *apTrabajador;
do{
cout<<"\t Indique el numero de Trabajadores: ";
cin>>nT;
}while(nT<=0||nT>=40);
apTrabajador=new cTrabajador[nT];
for(i=0;i<nT;i++)
apTrabajador[i].leer();/
system("cls");
cout<<"\t Lista de Trabajadores"<<endl;
for(i=0;i<nT;i++)
apTrabajador[i].imprimir ();
delete[]apTrabajador;
system("pause");
return 0;
I used to program in C++, the program stores nT number of arrays in order to have all the data of the "trabajadores" so im Trying to make sense to the syntaxis i learned in C++ now in C# i would gladly welcome some help
Registro is a method in class that asks for data to the user, Imprimir is another method in the same class to print the stored data into the array
namespace Poligono
{
class Program
{
static void Main(string[] args)
{
int i,n;
int[] Calculos;
Console.WriteLine("\n Insert the number of items");
string entry = Console.ReadLine();
int.TryParse(entry, out n);
Calculos = new int[n];
for (i = 0; i < n; i++){
VolumenPrisma.Registro(); //needs to be stored in "n" arrays
}
for (i = 0; i < n; i++){
VolumenPrisma.Imprimir(); //needs to be stored in "n"
}
}
}
}
/* The class is */
namespace Poligono
{
public class VolumenPrisma
{
public static int Apotema, TamLado, NumLados, Altura, Lado;
public static int Perimetro, Area, Volumen;
public static int cPerimetro (int NumLados, int Lado){
int P;
P=(NumLados*Lado);
return P;
}
public static int cVolumen(int Area, int Altura)
{
int V;
V = Area * Altura;
return V;
}
public static int cArea(int Perimetro, int Apotema)
{
int A;
A = (Perimetro * Apotema)/2;
return A;
}
public static void Registro(){
Console.WriteLine("Indique No. de lados: ");
VolumenPrisma.Lado = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique tamanio de apotema: ");
VolumenPrisma.Apotema = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique tamanio de lado: ");
VolumenPrisma.TamLado = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique la altura: ");
VolumenPrisma.Altura = Int32.Parse(Console.ReadLine());
Console.WriteLine("----------------------------------------");
Perimetro= cPerimetro(TamLado, Lado);
Area = cArea(Perimetro, Apotema);
Volumen = cVolumen(Area, Altura);
}
public static void Imprimir() {
Console.WriteLine("Lados= {0} ", Lado);
Console.WriteLine("Tam Lado = {0} ", TamLado);
Console.WriteLine("Apotema = {0} ", Apotema);
Console.WriteLine("Perimetro = {0} ", Perimetro);
Console.WriteLine("Area = {0} ", Area);
Console.WriteLine("Volumen = {0} ", Volumen);
}
}
}
Upvotes: 2
Views: 86
Reputation: 566
First you need an array of instances of VolumenPrisma
to work with, as you need to store somewhere the input data.
VolumenPrisma[] Calculos = new VolumenPrisma[n]();
Then you "register" (get the input parameters) these data by using:
for (i = 0; i < n; i++)
{
Calculos[i] = new VolumenPrisma(); // create a new instance to store the data
VolumenPrisma.Registro(Calculos[i]); //get the parameters for this prisma
}
for (i = 0; i < n; i++)
{
VolumenPrisma.Imprimir(Calculos[i]); //Print results for that prisma
}
The full code would be like:
namespace Poligono
{
class Program
{
static void Main(string[] args)
{
int i, n;
VolumenPrisma[] Calculos;
Console.WriteLine("\n Insert the number of items");
string entry = Console.ReadLine();
int.TryParse(entry, out n);
Calculos = new VolumenPrisma[n];
for (i = 0; i < n; i++)
{
Calculos[i] = new VolumenPrisma(); // create a new instance to store the data
VolumenPrisma.Registro(Calculos[i]); //get the parameters for this prisma
}
for (i = 0; i < n; i++)
{
VolumenPrisma.Imprimir(Calculos[i]); //Print results for that prisma
}
}
}
}
To work with static methods in VolumenPrisma class, you need to pass as parameter an instance of VolumenPrisma and modify the accesors for the instance fields.
public class VolumenPrisma
{
public int Apotema, TamLado, NumLados, Altura, Lado;
public int Perimetro, Area, Volumen;
public VolumenPrisma()
{
}
public static int cPerimetro (int NumLados, int Lado)
{
int P;
P=(NumLados*Lado);
return P;
}
public static int cVolumen(int Area, int Altura)
{
int V;
V = Area * Altura;
return V;
}
public static int cArea(int Perimetro, int Apotema)
{
int A;
A = (Perimetro * Apotema)/2;
return A;
}
public static void Registro(VolumenPrisma prisma)
{
Console.WriteLine("Indique No. de lados: ");
prisma.Lado = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique tamanio de apotema: ");
prisma.Apotema = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique tamanio de lado: ");
prisma.TamLado = Int32.Parse(Console.ReadLine());
Console.WriteLine("Indique la altura: ");
prisma.Altura = Int32.Parse(Console.ReadLine());
Console.WriteLine("----------------------------------------");
prisma.Perimetro = cPerimetro(prisma.TamLado, prisma.Lado);
prisma.Area = cArea(prisma.Perimetro, prisma.Apotema);
prisma.Volumen = cVolumen(prisma.Area, prisma.Altura);
}
public static void Imprimir(VolumenPrisma prisma) {
Console.WriteLine("Lados= {0} ", prisma.Lado);
Console.WriteLine("Tam Lado = {0} ", prisma.TamLado);
Console.WriteLine("Apotema = {0} ", prisma.Apotema);
Console.WriteLine("Perimetro = {0} ", prisma.Perimetro);
Console.WriteLine("Area = {0} ", prisma.Area);
Console.WriteLine("Volumen = {0} ", prisma.Volumen);
}
}
But I agree all this sounds a bit weird, and it would be a lot easier to work with not static methods.
Upvotes: 1