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

  • Context: Comp Sci 
  • Thread starter Thread starter Naldo6
  • Start date Start date
  • Tags Tags
    Arrays C++
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment to create a C++ program that calculates the depreciation of an item over ten years based on a given percentage. Participants focus on data entry validation, calculations, and the use of arrays in the implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • A participant describes their program requirements, including data validation and specific output conditions based on depreciation values.
  • The participant expresses uncertainty about the correct use of arrays, noting issues with corrupted variables in their program.
  • Another participant points out an array overflow issue, explaining that C++ arrays are zero-indexed and that accessing an index equal to the array size is out of bounds.
  • The original poster acknowledges the feedback and indicates they will adjust their code to use the correct array indexing.
  • A later reply suggests that the proposed changes should improve the program's functionality.

Areas of Agreement / Disagreement

Participants generally agree on the importance of proper array indexing in C++. There is a consensus on the need for corrections, but the overall effectiveness of the initial program remains unresolved.

Contextual Notes

Participants discuss potential issues with memory access and the implications of array indexing in C++. The original poster's program is noted to produce the desired output despite the identified issues, indicating a reliance on specific memory conditions.

Who May Find This Useful

This discussion may be useful for students learning C++ programming, particularly those working on array manipulation and data validation in their assignments.

Naldo6
Messages
102
Reaction score
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
ok, but i am asking for another opinion...

anyone know?...
 
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.
 
ok ty.. i use valorfinal[year] and also change my for to

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

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
6
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K