Increase number and show it on screen in c ++

0

I'm starting to program in c ++ and I was trying to make a program that would raise all the whole numbers to the square from 1 to 10. The thing is that when I execute it I get results that nothing to see. Here is my program to see if you can help me correct it. if they correct it, explain to me what my mistake or errors were.

#include <iostream>
#include <math.h>
#include <conio.h>


using namespace std;
    int main () {
        int a, b[9], c=0; 
        for (a>=1;a<=10;){ 
            pow(a,a)==b[c];
            cout<<b[c]<<endl; 
            a++;
            c++;
        } getch();
        return 0;
    } 
    
asked by karantooo 08.09.2018 в 05:45
source

3 answers

1

Karantoo. Welcome to StackOverflow. This is a possible solution to your problem.

#include <iostream>
#include <math.h>

int main(){
    for(int i = 1; i <= 10; ++i)
    {
        std::cout << pow(i, 2) << '\n';
    }
    return 0;
}

I explain. Within the cycle for , which starts in 1 and repeats 10 times the result of the function pow is printed in console. Which takes as the first argument the base number (in this case the value stored in the variable i of the cycle for , the second argument is the exponent to which you want to raise the base. You can read more about using the pow function on this link .

Maybe one of the reasons why your program does not work is because when you declare the variables you are not assigning them any value. We can observe it in the following way:

int main(){
    int a, b[9], c=0;
    cout << "a " << a << endl;
    for(int i =0;i <  9;++i)
    {
        cout << "b " << b[i] << endl;
    }
    cout << "c " << c << endl;
    return 0;
}

Which compiles correctly, but by running it twice we get the following:

➜  ~ ./a.out     
a 22099
b 877676960
b 32758
b 0
b 0
b 2111314656
b 22099
b 2111314032
b 22099
b 1747998336
c 0
➜  ~ ./a.out
a 21922
b 2117204384
b 32576
b 0
b 0
b 1325120224
b 21922
b 1325119600
b 21922
b -1271446880
c 0
➜  ~ 

As you can see the variable a and the array of b[] are not assigned a value before using them, doing so generates a value at run time.

    
answered by 08.09.2018 в 09:42
0

What happens is that you are not squared.

pow(a,a)==b[c];

should be upside down and squared

b[c] = pow(a, 2);

I'll give you an example:

int main()
{
    int b[11];
    for(int i = 1; i < 11; i++){
        b[i]=pow(i,2);
        cout<<b[i]<<endl;
    }
    return 0;
}

Greetings

    
answered by 08.09.2018 в 08:37
0

You have a remarkable collection of bugs in such a short code:

  • The header <math.h> is from not from . 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;
} 
    
answered by 10.09.2018 в 09:05