Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: m@o_614 en 26 Junio 2012, 00:42 am



Título: operaciones aleatorias
Publicado por: m@o_614 en 26 Junio 2012, 00:42 am
Saludos tengo que hacer un programa que me genere operaciones aleatorias con numeros aleatorios, tengo entendido que la función random se utiliza así para ue me genere los numeros pero no se como hacer para que me de +, - *, / al azar

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define OPERACIONES 5
  5.  
  6. int main()
  7. {
  8.    int i,numero,numero2;
  9.    char operacion;
  10.    srand(time(NULL));
  11.    for(i=0;i<OPERACIONES;i++)
  12.    {
  13.        numero = rand()%11;
  14.        numero2 = rand()%11;
  15.        operacion = rand()%???????;// Aqui no se que poner
  16.        printf("%d  %d\n",numero,operacion,numero2);
  17.    }
  18.    return 0;
  19. }
  20.  


gracias


Título: Re: operaciones aleatorias
Publicado por: nelxiton en 26 Junio 2012, 01:17 am
HOLA QUE TAL MIRA MASOMENOS PUEDES UTILIZAR DE ESTA FORMA EL GENERA NUMEROS ...

DISCULPA POR NO HACERLO COMO DERREPENTE LO DESEES ESQUE ESTOY APURADITO...  :D


Código
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "conio.h"
  4. #include "stdlib.h"
  5. #define N 10
  6. /*
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #define OPERACIONES 5
  11. */
  12.  
  13. using namespace System;
  14.  
  15. int main(array<System::String ^> ^args)
  16. {
  17.   int mtemperatura;
  18.   Random r;
  19.  
  20.   printf("GENERANDO NUMEROS\n");
  21.  
  22.   for(int f=0;f<N;f++)  
  23.   mtemperatura=r.Next(1,458);
  24.   printf("IMPRESION DE NUMEROS\n");
  25.   for(int i=0;i<N;i++)
  26.   {
  27. printf("%d", mtemperatura);
  28.         printf("\n");
  29.   }
  30. _getch();
  31.    return 0;
  32. }


Título: Re: operaciones aleatorias
Publicado por: maxim_o en 26 Junio 2012, 02:42 am
Para generar aleatoriamente el  + - / * puedes asignarle un numero y generas aleatoriamente ese numero y segun sea haces una operacion u otra...

Por ejemplo:
+ = 0
- = 1
* = 2
/ = 3

Generas un numero aleatorio entre 0-3
y con un switch.
SI es 0 printf( "%d + %d = %d",num1,num2,num1 + num2);
Si es 1 printf("%d - %d = %d",num1,num2,num1-num2);
etc