1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with Arrays in C++

  1. Nov 9, 2008 #1
    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 dont 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");

    printf("Entre Valor Del Equipo:\n$");

    printf("\nEntre El Porciento De Depreciacion:\n");




    printf("\nAl Year %i La Depreciacion= $%.2f y Valor Nuevo= $%.2f\n\n",year,depreciacionfinal[year],valorfinal[year]);
    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");

  2. jcsd
  3. Nov 10, 2008 #2
  4. Nov 10, 2008 #3
    ok, but i am asking for another opinion...

    anyone know?...
  5. Nov 11, 2008 #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.
  6. Nov 11, 2008 #5
    ok ty.. i use valorfinal[year] and also change my for to

    for(year=0;year<size;year++) and correct it
  7. Nov 12, 2008 #6
    That should work better.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Help with Arrays in C++
  1. C++ Help, 2-D Arrays (Replies: 3)

  2. Array help in C (Replies: 12)