Sum of digits in random generated numbers in array

Write a program with a function main () and selecting a menu of functions:
Generate a programming-random number generator data for lottery ticket / max 100 / with six-digit numbers and store them in an array
-Overwrite generated a new array and sort this array in ascending order and display output
-Counting and display output and numbers of all "happy" six digit lottery tickets /these numbers which sum of the first 3 digits is equal to the last three/
-Save in the array and display output sequence numbers of downloaded "lucky" lottery tickets


1
2
3
4
5
6
7
{srand((unsigned)time(0));
   int i;
   for(i=0; i<100; i++){
        numbers = (rand()%100000)+100000;
		numbers=array[i];
		cout << numbers << endl;}
}


-Counting and display output and numbers of all "happy" six digit lottery tickets /these numbers which sum of the first 3 digits is equal to the last three/


I dont understend how to find these numbers in array which sum of the first 3 digits = sum of the last 3
How to do it?
Last edited on
I think you've messed up on your code...

1
2
3
4
5
6
7
8
srand((unsigned)time(0));
int i;
for(i=0; i<100; i++)
{
	numbers = rand() % 1000000 + 100000;
	array[i] = numbers;
	cout << numbers << endl;
}


Also, you can separate out the digits by using the modulo operator. For example, you have the number 932041.

932041 % 10 = 1
93204 % 10 = 4
9320 % 10 = 0
932 % 10 = 2
93 % 10 = 3
9 % 10 = 9
Last edited on
932041

int a_ticket[6] = "932041" // Read from a file or generated during run-time


// get first ticket value, made-up of two parts; low&high
N=0;
lowTikNum = a_Ticket[n] a_Ticket[n+1] + a_Ticket[n+2]
HighTikNum = a_Ticket[n+3] a_Ticket[n+4] + a_Ticket[n+5]
if lowTikNum = HighTikNum then Happy = TRUE

//Get next ticket number ie Next 6 INTs ( 246753) - into a_ticket[]
N++;


You can generate , say, 600 INT values at the start of the program
and place the INTS into an INT array[] named something like...
a_Ticket[600] = 0;
for( j=0, j++; j<600 )
a_Ticket[j] = RND(0) MOD 10;

Now that you have 600 INTs ( or 100 - 6 digit values) with values of 0-9,
you can cut and process 6 INTs at a time to get a ticket number
and cut that value in half ( 1-3 & 4-6 ) for comparison for happy numbers

Then get the NEXT SIX values and process again the same way, until you've processed all 600 INT values in the array.

= = = =
Now with 600 INTs in a single array, you can now SORT the array
and move it into a new array in ascending order.

// next step is to preform a BUBBLE SORT
Take the first six INTs from the array and place them into an INT var
Place this into the first position of the newly created ascending array
INT a_ASCENDING[100]=0;

a_ASCENDING[1]=932041;

Take the NEXT six INTs from the array and place it ABOVE or BELOW the
first value. ( in this case "246753" would be ABOVE " 932041 "

a_ASCENDING[1]=246753;
a_ASCENDING[2]=932041;

which is the reason for preforming a bubble sort of the ASCENDING array
and why the same ASCENDING array only requires 100 INT values.

SAVE and PRINT ( btw, the array is already saved "automatically! )

in a loop just print the a_Ascending[] array from 0-99 and UR done.

in short-
(1) generate 600 random values between 0-9
(2) store them into an array
(3) use your array indexing to group the values by 6 for tickets
or by 3 for happy number comparisons
(4) bubble sort your array in groups of six into a new, smaller array
(5) print the new array & stop

PS, I just noticed the MAX 100 tickets, remember to ASK the user how many tickets to generate.
n=0;
" how many tickets do you want? n
if n < 0 or n > 100 then n = 100

Last edited on
Ok here is the code, one friend told me how to do this with lucky numbers, but i dont know how to connect algoritm with random generatet numbers and dont know how to sort it, can someone repair the code
when i choice option 2 from menu i seeing this
6
5
4
3
2
1
sum of the first 3=dum of the last 3

The function with lucky numbers i probably wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int numbers;
int array[100];

void generator()//generate random numbers
{srand((unsigned)time(0));
 int i;
    for(i=0; i<100; i++){
       numbers = (rand()%900000)+100000;
       array[i]=numbers;
          cout << numbers << endl;}}
          
          

int lucky()//display lucky numbers
{
   int a(123456),temp,del = 10;int b(123321),sumpar(0),sumpos(0);
   cout<<a%10<<endl;
   for(int i(0);i<5;i++)
     {
      temp = a/del;
      del = del*10;
      cout<<temp%10<<endl;
     }

for(int i(0);i<6;i++)
   {
    temp = b/del;
    if(del < 1000)sumpos += (temp%10);
      else sumpar += (temp%10);
       del = del*10;
    }
  
  if(sumpar == sumpos)
  cout<<"sum of the first 3=sum of the last 3";

     return EXIT_SUCCESS;
}

int menu()
{
int choice;
cout<<"\n_______________MENU_____________________";
cout<<"\n 1. Generirate random numbers";
cout<<"\n 2. Display lucky numbers";

do
{
cout<<"\n Choice: ";
cin>>choice;
}
while(choice<1||choice>2);
return(choice);
}
int main()
{
int i;
do
{
i=menu();
switch(i)
{
case 1: generator();break;
case 2: lucky();break;
}

}
while(i!=3);
return 0;
}
You've got the number generating function correct. Your second function is totally wrong. It doesn't even check the array where you stored the numbers you generated.

EDIT: Sorry, just noticed you were trying to test out your algorithm on the numbers 123456 and 123321? Well, the algorithm is totally wrong.

What you should do is to create a copy of the number. Inside a loop, find the value of the copy modulo 10, then divide the copy by 10. Sum the first 3 values from the modulo operation in one variable, and sum the next three in another variable.

If the two sums are the same, then it's a lucky number.
Last edited on
Topic archived. No new replies allowed.