You have a remarkable collection of bugs in such a short code:
- The header
<math.h>
is from c not from c ++ . The headers of C have a version adapted to C ++ that has the prefix c
and lacks extension. If you really need to use the header of C <math.h>
(which will never be the case) you should use its C ++ equivalent <cmath>
. Read this thread to find out why.
- The header
<conio.h>
does not exist in C ++ and even if it is standard C, see this question to know why.
- There is no obligation to use the
using namespace std;
clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.
- Try to initialize the variables; in C ++ a value is not assigned to the variables unless the programmer asks for it explicitly; if they are not initialized, the variables will obtain indeterminate values (usually residual values of memory) and this will cause unexpected behavior in the program.
- Avoid abusing
std::endl
(because it can cause performance problems) and favors the use of the explicit line break ( \n
). Read this thread to find out why.
- Try favor pre-increment versus post-increment .
In addition to those failures, you have the following errors:
Your loop is incorrect.
The %% C ++% loop is divided into three parts:
for ( inicialización; condición de salida; sentencia de avance) {
código
}
In the for
the expressions that initialize the variables that will be used for the loop to roll are usually put. In the inicialización
you should write an expression that results in true or false , the latter result being the one that stops the loop. The condición de salida
is an expression that can modify data that could influence the operation of the loop.
In your loop:
int a, b[9], c=0;
for (a>=1;a<=10;)
In the sentencia de avance
you have the expression inicialización
, the a>=1
is executed only once and in your case you will evaluate if inicialización
is greater than or equal to a
returning a true value o false that is lost if it is not stored anywhere. Your 1
is condición de salida
Check if a<=10
is less than or equal to a
and if it is false the loop will end, but since you have not initialized 10
, this variable could have a value greater than a
as soon as you reach the loop.
The correct way to write your loop would be:
int b[9], c=0; \ <-- 'a' ahora forma parte del bucle for.
for (int a = 0; a < 10; ++a)
\ ~~~~~~~~~ ~~~~~~ ~~~ <-- Avance
\ \ \
\ \ \-- Condición de salida
\ \
\ \-- Inicialización
And it could be read as " Since 10
is a
and while 0
is less than a
, add 10
to 1
".
You compare two numbers, without important the result.
In the expression:
pow(a,a)==b[c];
Elevas a
to the power of a
and compare
strong> if that calculation is equal to the content of the formation
a
in position
b
, that by not having initialized
c
we can not deduct the result (although it is very likely that it is false ) but it does not matter, because you discard the result, surely what you wanted to do was assign (not compare):
b[c] = pow(a,a);
\ ~~~~~~ ~~~~~~~~ <-- Resultado de elevar 'a' a la potencia de 'a'
\ \
\ \-- Asigna a la posición 'c' de la formación 'b'
Proposal.
Keeping in mind all of the above, your code might look like this:
#include <iostream>
#include <cmath> // <-- <cmath>, no <math.h>
int main () {
using namespace std; // <-- cláusula using dentro de main
int b[9]{};
\ vvvvv <-- variable 'a' inicializada a 0.
for (a = 1; a < 11; ++a){ // Bucle de 1 a 10.
b[a - 1] = pow(a, 2);
cout << b[c] << '\n';
}
return 0;
}