You are here:

C++/Array Problem

Advertisement


Question
sry to bother u again, but i have made some progress, i already got the salaries on the array...no I need to mutlipy the array for the discount, but when i multiply it it doesnt works...i just prints the salaries withouth being multiplied:

#include <stdio.h>

#define DESC 0.125



int salario[4], cont, a;
void main ()
{
     
  for (a=0; a<=4; a++){
        printf("Introduzca Salario [%d]: ", a);
     scanf("%d", &salario[a]);
  }

  printf("\n");

  {

  (salario[a]*0.125);

  }

for (a=0; a<=4; a++){
printf("salario [%d]: %d\n",a, salario[a]);

}
}

Answer
I notice several things about your code. First up is that you are writing C code and not C++ (well, it _is_ valid C++, but is using only C features), but you are asking a C++ expert. This leads me to question which language you wish me to concentrate on. If you are writing C then please say so in your question, otherwise, as I am answering as a C++ expert I will default to assuming C++...or just left wondering.

Secondly you seem to be relying on global variables for no really good reason that I can see. Global variables are evil. Try your utmost _not_ to use them.

Now, having restored you indentation - I hope I restored it and that posting through AllExperts mangled it, and this is not how you really indent your code - as it arrived somewhat messy I can see what is going on. By the way, I, and many, many others do _not_ like K & R indentation style - where the opening brace is on the end of the line of the statement to which it pertains - it is not that easy to read (I can never find the opening brace). However this is a matter of style, which is a personal thing.

So let's run through your main function. First you ask the user on standard output and read from standard input four salary values.

Next you clear the line by printing a new line to standard output.

Then you start a local scope section _not_ in a loop and multiply salary at index a by 0.125. You do this calculation and do not store the result anywhere.

So there are two things wrong. The first is that you perform a calculation and do not store the result:

       p*q;

produces a third temporary result value that is not assigned to be stored anywhere, specifically p and q are _not_ modified, hence:

       salario[a]*0.125;

does not modify salario[a]. You can either assign the value somewhere:

       x = salario[a]*0.125;

or more likely:

       salario[a] = salario[a]*0.125;

Or use a combined assignment and operation operator to perform the multiplication and assignment back to salario[a] in one go:

       salario[a] *= 0.125;

Which is equivalent to the previous example. Note that all cases the results are 12.5% of the original salary - so maybe you did not wish to overwrite the original salary with this rather drastic reduction after all - maybe it should be stored in some other place, like the initial example, but probably another array. The other point is you define a pre-processor macro name for the 0.125 value and do not use it, so maybe you intended something like:

       salario_desc[a] = salario[a]*DESC;

Your second problem is that the variable a retains the final value from the previous for loop - which is the value 4 - which is out of bounds as a loop index. Technically it is allowed as the small print for arrays dictates that you can access one past the end, however this is intended more for detecting end of run conditions, and your values of interest are at indexes 0, 1, 2 and 3.

So not only did you perform a calculation and then throw away the result, the value of salary you used was out of range nonsense anyway.

In fact it may well have been zero as salario is a global array with external linkage - however although you need to be able to access one past the end, I do not know if it is a requirement to actually initialise this element or for it to actually exist. The one past the end index of the array is therefore likely to be the memory slot used for any following variables - such as cont in this case, which is also initialised to zero. If you made your array values local automatic variables by moving their definitions inside the body of main then they would not be initialised and would therefore contain garbage to start with.

So it seems to me that either you require another loop in which to perform your calculations on the four salario values or you should be doing your calculations in the loop in which you read the values. You also need to think about _where_ you wish the results of these calculations to be stored and remember to store them someplace.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.