mercredi 21 octobre 2015

Randomly choose two elements from array and swap values in C

I'm writing a program where part of the instructions is to randomly choose two different ord[] elements and then swap their contents. I'm firstly having issues even generating a random element from the array, let alone swapping them (I can't even get it to print the random values). What am I doing wrong? I'm sorry if I'm messing everything up completely. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
#define MMAX 5;
#define ORD_SIZE ( sizeof((ord)) / sizeof((ord[0])) )
int randomOne = 0;
int randomTwo = 0;
int ord[MMAX];

srand(time(NULL));
randomOne = ord[rand() % ORD_SIZE];
randomTwo = ord[rand() % ORD_SIZE];
printf("%d", randomOne);
printf("%d", randomTwo);

int tmp = 0;
tmp = randomOne;
randomOne = randomTwo;
randomTwo = tmp;
printf("%d", ord[randomOne]);
printf("%d", ord[randomTwo]);

return 0;}




Aucun commentaire:

Enregistrer un commentaire