-2

To start with, l'm an early beginner programmer and would like some help with this please.

I have written the following code, which from what l have tested generates:

  • 5 random numbers between: 1 and 39 //num1gen to num5gen - e.g.group A
  • 1 random number between: 1 and 14 //Thunderball - e.g.group B

I only want to cout<< the group A numbers in ascending order and l am not sure what coding is required to add this feature.

I still need the random numbers generated to be placed in the integer names shown below:

i.e

  • num1gen = 12
  • num2gen = 24
  • num3gen = 3
  • num4gen = 5
  • num5gen = 32
  • Thunderball = 12

ERROR: ISO C++ Forbids deceleration of 'i' with no type [-fpremissive] on line 16.

The code l have done so far with the help of the users below:

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>

using namespace std;

int main()

{

 srand(time(NULL));

std::vector<int> numA(5);
srand( time(NULL) );
for( auto i(0); i < numA.size(); ++i )    //line no 16 error
numA[i] = (rand()%49+1);

  int num1gen=(rand()%49+1);    // this is the value of ball no.1
  int num2gen=(rand()%49+1);    // this is the value of ball no.2
  int num3gen=(rand()%49+1);    // this is the value of ball no.3
  int num4gen=(rand()%49+1);    // this is the value of ball no.4
  int num5gen=(rand()%49+1);    // this is the value of ball no.5


    std::sort(numA.begin(), numA.end());

num1gen=numA[0];
num2gen=numA[1];
num3gen=numA[2];
num4gen=numA[3];
num5gen=numA[4];

 cout<<num1gen<< ", "<<num2gen<< ", "<<num3gen<< ", "<<num4gen<< ", "
 <<num5gen<< " ";"

    return 0;
  }
Mike Ericson
  • 23
  • 1
  • 2
  • 7

2 Answers2

2

If you create the numbers in A as a vector, there is an algorithm header with sort, so somthing like:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numA(5);
    srand( time(NULL) );
    for( unsigned int i(0); i < numA.size(); ++i )
        numA[i] = (rand()%49+1);
//After you create the vector and do your test that they're not equal
    std::sort(numA.begin(), numA.end());

    return 0;
}

The std::sort() is included in the #include <algorithm> header.

TriHard8
  • 592
  • 6
  • 18
  • Thanks for that l will try it! However, will this place the random numbers generated into num1gen, num2 gen ...... to num5gen? Because for it to be compatible to the remainder of the programme l have written l would require these numbers generated to be stored inside those memory locations. Due to them being referenced and read multiple times throughout. – Mike Ericson Feb 20 '15 at 19:49
  • @MikeEricson - No it won't. Did you edit your original question with that requirement? – TriHard8 Feb 20 '15 at 19:51
  • There is no sort methode in `vector`. You have to use `void sort (RandomAccessIterator first, RandomAccessIterator last)` – TNA Feb 20 '15 at 19:51
  • I have not edited any of the content of my question, only the indentation of the code. Thanks – Mike Ericson Feb 20 '15 at 19:54
  • @TriHard8 Change `numA.sort(numA.begin(), numA.end());` to `std::sort(numA.begin(), numA.end());` – TNA Feb 20 '15 at 19:55
  • @MikeEricson okay. I wasn't the only one that missed that statement at least. – TriHard8 Feb 20 '15 at 19:56
  • @TNA good catch, I was typing in a hurry. It doesn't solve his dilemma anyway. – TriHard8 Feb 20 '15 at 19:58
  • This is how l was doing it before just to show you how l was previously doing it... if(num1gen – Mike Ericson Feb 20 '15 at 20:02
  • @MikeEricson it's not good coding practice, but you could always take my code and assign num1gen to numA[0] and do the same for the other 5 and print out that sorted list. I would say that's not good coding practice though. Depending how much other code you have, it may be worthwhile to just hold everything in the vector and modify the rest of your code to conform. – TriHard8 Feb 20 '15 at 20:07
  • @TriHard8 That is exactly what l would like to do! As l am a real beginner, l am completely happy to do this (still learning). How would l assign the integer value held in num1gen to numA[0] and num2gen to numA[1] etc...? Thanks for the help – Mike Ericson Feb 20 '15 at 20:10
  • @MikeEricson you would just do num1gen = numA[0]; num2gen = numA[1]; ... ; num5gen = numA[4];. Do that before you sort the vector. Again, this is TERRIBLE programming methodology and if you want to learn, you should redo the rest of the code to conform. After you make that assignment, the num1gen will not link back to numA[0]. You'll be creating a completely separate variable that will not maintain a dynamic link. – TriHard8 Feb 20 '15 at 21:19
  • @MikeEricson also, if you feel that this is the best answer to your question, please click the checkmark to the left of my answer. Thanks. – TriHard8 Feb 20 '15 at 21:20
  • Thanks, l understand the issues that will arise and will take that into consideration. – Mike Ericson Feb 20 '15 at 21:21
  • I changed my initial code above to what you kindly gave me. I feel that l may have not put each part in the correct place. Could you reorganise it for me? Or suggest any parts that have not been place in the correct locations? Thank you. – Mike Ericson Feb 20 '15 at 21:24
  • @MikeEricson Since you're not using c++11, you can't use `auto` on line 16, so change that to `unsigned int`. – TriHard8 Feb 20 '15 at 21:52
  • @TriHard8 Works like a dream. Thank you – Mike Ericson Feb 20 '15 at 22:20
1
 #include <random>
 #include <vector>
 #include <algorithm>
 #include <numeric>

 int main()
{
 std::vector<unsigned> balls(39);
 std::iota(balls.begin(), balls.end(), 1);
 std::shuffle (foo.begin(), foo.end(), std::mt19937(std::random_devic{}()));
 balls.resize(5);
 std::sort(balls.begin(), balls.end());
 std::cout << "Balls: "
 std::copy(balls.begin(), balls.end(), std::ostream_iterator<unsigned>{std::cout," "})
}
TNA
  • 2,595
  • 1
  • 14
  • 19
  • I have tried this and recieved this message. #ifndef _CXX0X_WARNING_H #define _CXX0X_WARNING_H 1 #if __cplusplus < 201103L #error This file requires compiler and library support for the \ ISO C++ 2011 standard. This support is currently experimental, and must be \ enabled with the -std=c++11 or -std=gnu++11 compiler options. #endif #endif – Mike Ericson Feb 20 '15 at 19:52
  • @Mike Ericson I used C++11 so you have to turn on C++11 support as the message says. – TNA Feb 20 '15 at 19:54
  • Unfortunately l can not find that option. I am using codeblocks. Thanks for your response. – Mike Ericson Feb 20 '15 at 19:57
  • You're not getting tired of typing `std::` every 2 cm in your code? – ott-- Feb 20 '15 at 22:01