Depreciation Calculator in C++: Calculate Values & Validate Data Using Arrays

  • Comp Sci
  • Thread starter Naldo6
  • Start date
  • Tags
    Arrays C++
In summary: Just remember to do the same for the depreciacionfinal array as well. Arrays in C++ start at 0, so the last value you can access is size-1. So the correct for loop would be for(year = 0; year < size; year++). Good luck with your program!In summary, the conversation discusses a program that shows the depreciation of an item over 10 years using data entry validation, calculations, and arrays. The programmer is having trouble with their code and is seeking advice on how to use arrays correctly. They are informed of the proper way to use arrays in C++ and are advised to make the necessary changes to their code.
  • #1
Naldo6
102
0
My Homework is write a program to show the depreciation of an item each year during 10 years, for a porcentage given. The prgram shoul include:
1- Data entry validation
2-Calculate all the values for each year,and tehn of calculate all the values should show de results.
3-If the final value at the end of the year is less than the 72% of the original value of the original value of the item, should show "Valor Por debajo de lo deseado".
4- Use arrays.

I have done my program yet, but i don't know if i use the apropiate way with the arrays, because the program give me and corrupted variable on the valorfinal and depreciacionfinal variables, but if i ignore the error, in the screen it is showed what the professor wants... i only need to cheq if i use the arrays in the appropoate way to do this programs.

Here is my program:

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

void main()
{

const int size=10;
float valorfinal[size];
float depreciacionfinal[size];
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",&valor);
}while(!(valor>0));


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

valorinicial=valor;

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

depreciacionfinal[year]=depreciacion;
valorfinal[size]=valor;


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

}
 
Physics news on Phys.org
  • #3
ok, but i am asking for another opinion...

anyone know?...
 
  • #4
You are overflowing your array. Remember that C++ counts from zero so an array of size ten goes from valorfinal[0] to valorfinal[9], NOT valorfinal[10]. When you do the following: valorfinal[size]=valor, you are one float past the end of valorfinal since size is equal to 10. This will often work when that memory location is not used for anything (hence your correct result). However the program will crash in unexpected ways at other times.
 
  • #5
ok ty.. i use valorfinal[year] and also change my for to

for(year=0;year<size;year++) and correct it
 
  • #6
That should work better.
 

1. What is an array in C++?

An array in C++ is a data structure that allows for the storage of multiple elements of the same data type in a sequential manner. It is a fixed-size container, meaning the number of elements it can hold must be specified at the time of its creation.

2. How do I declare an array in C++?

To declare an array in C++, you need to specify the data type of the elements it will hold, followed by the name of the array and the number of elements it will contain in square brackets. For example: int myArray[5];

3. What is the difference between an array and a vector in C++?

Arrays and vectors are both used to store multiple elements in C++, but they have some key differences. Arrays have a fixed size and cannot be resized, while vectors can dynamically resize themselves. Additionally, vectors offer additional functionality such as the ability to add or remove elements.

4. How do I access elements in an array in C++?

To access elements in an array in C++, you need to use the index of the element you want to access within square brackets after the name of the array. For example: myArray[3] will access the 4th element in the array (arrays in C++ are zero-indexed).

5. Can I use loops to iterate through an array in C++?

Yes, loops can be used to iterate through an array in C++. The most commonly used loop for this purpose is the for loop, where you can use the loop counter as the index to access each element in the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
Back
Top