Τετάρτη 11 Μαΐου 2016

Lucky six (in c not plusplus)


The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Ritchie and Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11. The original PDP-11 version of Unix was developed in assembly language. The developers were considering rewriting the system using the B language, Thompson's simplified version of BCPL.[9] However B's inability to take advantage of some of the PDP-11's features, notably byte addressability, led to C.
The development of C started in 1972 on the PDP-11 Unix system[10] and first appeared in Version 2 Unix.[11]
From wikipedia

Ένα προγραμματάκι, randomizer, που παράγει 6 τυχαίους αριθμούς (αν θέλετε για να παίξετε Lotto) και κατόπιν ταξινομεί τον πίνακα που τους αποθηκεύει με ένα nested For Loop, quite elegant solution I'd say
The source code
#include <stdio.h>
#include <conio.h>
int newRandNumb();
int main(void){
int i=0;
srand(time(NULL));
i=0;
int y=0;
int chosenNums[5];
int generatedNum=0;

while (i<6){
generatedNum=newRandNumb();
if (generatedNum==0){
while(generatedNum==0){
generatedNum=newRandNumb();
}
}
if(i>0){
y=0;
do{
if (generatedNum == chosenNums[y]){
y=0;
generatedNum=newRandNumb();
while(generatedNum == chosenNums[y]){
generatedNum=newRandNumb();
}
}
y++;
} while (y <i);
}
chosenNums[i]=generatedNum;
i++;
}
printf("The lucky six are: ");
for (i=0;i<6;i++){
printf("%d,",chosenNums[i]) ;
}


printf("\n\nArranging the numbers in ascending order \n\n");

int orderArray[5];

i=0;
y=0;
int x=0;
int r=0;

for (i=0;i<6;i++){
for (y=0;y<6;y++){
if (chosenNums[i] < chosenNums[y] && y < i){
r=chosenNums[i];
chosenNums[i]=chosenNums[y];
chosenNums[y]=r;
} /*else if (chosenNums[i] > chosenNums[y] && y > i) {
r=chosenNums[y];
chosenNums[y]=chosenNums[i];
chosenNums[i]=r;
}*/
}
}
for (i=0;i<6;i++){
printf(" \nthe order is %d",chosenNums[i]);
}
getch();
return 0;
}
int newRandNumb(){
return (rand()%47);
}

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου