Comp Sci Depreciation Program in C++ with arrays

AI Thread Summary
The discussion focuses on modifying a C++ depreciation program to incorporate arrays, as requested by the professor. The original code calculates the depreciation of an item over ten years but does not utilize arrays to store values. Participants suggest creating arrays to hold depreciation values and item values for each year. Guidance is provided on how to index arrays correctly and print their contents. The conversation emphasizes the importance of validating input data and correctly implementing array usage in the program.
Naldo6
Messages
102
Reaction score
0
Hi, i have did my program ccorrectly, but my profesor ask me to do with arrays and i don't know how to do thisprogram wuth arrays. Can anyone helps please?... I talk with my profesor and see my prgram, and told me that it is all ok , but the only mistake is that i don't use arrays how the work is supossed to be done...

Little of the instructions...

This is a program to show the depreciation value of an item at some porcentage during the first 10 years. If the value of the item at the end of the year is less than 72% of the orignal value, must show "Value under Interested". You Must validate the entry data. Do it using arrays.



# include <stdio.h>
# include <math.h>

void main()
{

int year;
float valor,valorinicial,porciento,depreciacion;

printf("\nPrograma Para Determinar Depreciacion De Un Equipo\n\n");

do{
printf("Entre Valor Del Equipo:\n$");
scanf("\n%f",&valorinicial);
}while(!(valorinicial>0));

do{
printf("\nEntre Nuevamente Valor Del Equipo:\n$");
scanf("\n%f",&valor);
}while(!(valorinicial==valor));

do{
printf("\nEntre El Porciento De Depreciacion:\n");
scanf("\n%f",&porciento);
}while(!(porciento>=0));

for(year=1;year<=10;year++)
{
depreciacion=valor*porciento/100;
valor=valor-valor*porciento/100;

if(valor>=.72*valorinicial)
printf("\nAl Year %i La Depreciacion= $%.2f y Valor Nuevo= $%.2f\n\n",year,depreciacion,valor);
else
printf("Al Year %i Valor Por Debajo De Lo Deseado\n\n",year);
}

}
 
Physics news on Phys.org
The code is very simple, but what does your professor want you to use the arrays for?

Without your clarification, I'm going to guess that he wants you to store the information about each year in an array.

Code:
int dep_array[10];
int val_array[10];

for(year=1;year<=10;year++) {
depreciacion=valor*porciento/100;
valor=valor-valor*porciento/100;
[b]dep_array[year-1]=depreciacion
val_array[year-1]=valor
...[/b]

The -1 is used in the index because you have to remember that arrays in C are 0-based, and you're dealing with "years" here. So somewhere later in your code, to get the proper index for a year, you'd have to take the year you wanted data from and subtract 1 from it.

I'm not sure if that answers your question though.
 
Do you mean that?...

# include <stdio.h>
# include <math.h>

void main()
{
int dep_array[10];
int val_array[10];
int year;
float valor,valorinicial,porciento,depreciacion;

printf("\nPrograma Para Determinar Depreciacion De Un Equipo\n\n");

do{
printf("Entre Valor Del Equipo:\n$");
scanf("\n%f",&valorinicial);
}while(!(valorinicial>0));

do{
printf("\nEntre Nuevamente Valor Del Equipo:\n$");
scanf("\n%f",&valor);
}while(!(valorinicial==valor));

do{
printf("\nEntre El Porciento De Depreciacion:\n");
scanf("\n%f",&porciento);
}while(!(porciento>=0));

for(year=1;year<=10;year++)
{
depreciacion=valor*porciento/100;
valor=valor-valor*porciento/100;

dep_array[year-1]=depreciacion;
val_array[year-1]=valor;

if(valor>=.72*valorinicial)
printf("\nAl Year %i La Depreciacion= $%.2f y Valor Nuevo= $%.2f\n\n",year,depreciacion,valor);
else
{
printf("\nAl Year %i La Depreciacion= $%.2f y Valor Nuevo= $%.2f\n\n",year,depreciacion,valor);
printf("\t Valor Por Debajo De Lo Deseado\n\n",year);
}
}


}


but how do i print the arrays?... because my printf are of the variables valores and depreciation, but how do i print with arrays?... i thinks that that is what my profesor wants...
 
Yes, that's the general idea. If he wants you to print your arrays then you'd have to just call the index of the array you want to print. I'm not sure I'm understanding your question.

Read http://en.wikibooks.org/wiki/C_Programming/Arrays and see if that helps. Unless you clarify exactly what you need I'm not sure to further assist you.
 
Back
Top