Sorting random number array into primes

Hey guys, new to this site so sorry for anything im not supposed to be doing. I have just started c++ so still really dull with it. I have a assignment to program a random number generated array of 25 and sort it into order ascending from smallest to biggest. Then after that also indicate which of those random numbers now in order is a prime number.

I have done all except for when it goes to see if the number is prime or not. The problem is that the for loop for the prime sorter goes up with the loops. so say if i had 3x number 3 it would only try divide the number by the next number in the for loop. so it would do 3/1 first then for the 2nd number 3 the for loop would now equal 2 so it doesn't divide by 1 it misses out.

Just wandering if anyone could help. Not wanting to completely use advanced stuff just basic.

Thanks for any help.
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
//
#include "stdafx.h"
#include "iostream" 
#include "cmath" 
using namespace std; 

void main()
{
double Numbers[26],B, C, n;
int i, j, k;

cout<<"\n";
cout<<"\t"<<"Hi, Welcome to the random generator number sorter.....";
cout<<"\n";
cout<<"\n";
cout<<"\t";
for (i=1; i<=25; i++)
{
//Random 1 to 25
Numbers[i]=rand()%25+1;
cout<<Numbers[i]<<" "; 
}

cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
for (j=1; j<=23; j++)
{
for (i=1; i<=25-j; i++) 
{
if (Numbers[i]>Numbers[i+1]) 
swap(Numbers[i+1],Numbers[i]);
}
}
cout<<"\t";
for (i=1; i<=25; i++) 
{
cout<<Numbers[i]<<" ";											
}
cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
for (j=1; j<=23; j++) 
{
for (i=1; i<=25-j; i++) 
{
if (Numbers[i]>Numbers[i+1]) 
swap(Numbers[i+1],Numbers[i]);
}
}

cout<<"\t";

for (i=1; i<=25; i++)
{
	for(k=1; k<=Numbers[i]; k++)
	{
	B=Numbers[i]/k;
	C=int (Numbers[i]/k);

	if (C!=B)
{
cout<<"*"<<Numbers[i]<<" ";
}
cout<<Numbers[i]<<" ";
}
}
}
The cheaty way to do it would be to create a const array containing every prime number from 1 to 25 (2, 3, 5 , 7, 11, 13, 17, 19, 23), and check each random number against those numbers.
Indentation, please.

Why do you repeat lines 27--36 in 44--54?

Integer division discards remainder. 5/6 == 0. Use the modulo operator (%).

Line 56:
k=1. Every integer is divided by 1, so start with k=2.
k<=Numbers[index]. 42/42 == 1, so stop with k<Numbers[index].

Number is a prime if no number can divide it. Lets say that Number is 6:
6/2 does not print
6/3 does not print
6/4 prints *4
6/5 prints *4
Outer loop prints 4.

Lets do it other way. We presume that 9 is a prime. (bool prime=true;)
9/2: 9 could be a prime
9/3=3: Suddenly, we know that 9 is not a prime. (prime=false;) We can stop (break) the loop here.

The loop has completed. Did we mark the 9 as "not a prime" during it? If not, print "*".
Print "9 ".
Last edited on
Yay295 how would i compare two array with each other? Keskiverto Thanks for the feedback have changed a few things but im still having problems. Maybe it would be easier with my level to use Yay295's way.
This is now what i have done but still not working. it prints the prime then also prints it as a normal number. I'm unsure of how to stop it after it prints the prime and continue going thought the rest of the numbers. I tried using break but it still doesn't work.

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
sort.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h" 
#include "iostream" 



using namespace std; 

void main()
{

double int Numbers[26], Primes[]={2, 3, 5, 7, 11, 13, 17, 19, 23};
int i, j, k, l, m, n;
// 
cout<<"\n";
cout<<"\t"<<"Hi, Welcome to the random generator number sorter and prime number identifier.....";
cout<<"\n";
cout<<"\n";
cout<<"\n";
// 
cout<<"\t";
for (i=1; i<=25; i++)
{
//
Numbers[i]=rand()%25+1;
cout<<Numbers[i]<<" ";                                         
}
//
cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
for (j=1; j<=23; j++) 
{
for (i=1; i<=25-j; i++)  
{
if (Numbers[i]>Numbers[i+1]) 
swap(Numbers[i+1],Numbers[i]);
}
}
cout<<"\t";
for (i=1; i<=25; i++) 
{
cout<<Numbers[i]<<" ";											
}
cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
cout<<"\t";

for (i=1; i<25; i++)

{
bool printed = false;
for (n=1; n<9; n++)
{
if ( Numbers[i]==Primes[n])
cout<<"'"<<Primes[n]<<" ";
printed;
}

cout<<Numbers[i]<<" ";
}


cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
}
Last edited on
Managed to find some online chat and solved the mystery.

this is the solved code

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
76
sort.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h" 
#include "iostream" 



using namespace std; 

void main()
{

double int Numbers[26], Primes[]={2, 3, 5, 7, 11, 13, 17, 19, 23};
int i, j, k, l, m, n;
// 
cout<<"\n";
cout<<"\t"<<"Hi, Welcome to the random generator number sorter and prime number identifier.....";
cout<<"\n";
cout<<"\n";
cout<<"\n";
// 
cout<<"\t";
for (i=1; i<=25; i++)
{
//
Numbers[i]=rand()%25+1;
cout<<Numbers[i]<<" ";                                         
}
//
cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
for (j=1; j<=23; j++) 
{
for (i=1; i<=25-j; i++)  
{
if (Numbers[i]>Numbers[i+1]) 
swap(Numbers[i+1],Numbers[i]);
}
}
cout<<"\t";
for (i=1; i<=25; i++) 
{
cout<<Numbers[i]<<" ";											
}
cout<<"\n";
cout<<"\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"\n";
cout<<"\t";
bool printed;
for (i=1; i<26; i++)
{
printed = false;
for (n=1; n<9; n++)
{
if (Numbers[i]==Primes[n] )
{
printed = true;
cout<<"'"<<Primes[n]<<" ";
}
}
if (!printed)
{
cout<<Numbers[i]<<" ";
}
}

cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
}
Topic archived. No new replies allowed.