I was hoping I could get some help making my code more efficient. I'm a math major taking an Intro to Programming class, and I was asked to write a C program that generates:
A text file named random_ints.txt containing 1≤n≤1000 random integers, separated by commas, in the range [2,1000000] and;
A second file named primes.txt that contains a list of the prime numbers contained in random_ints.txt.
The basic idea of my program is to:
Create an array of n random integers and print them onto the first text file.
Identify all prime numbers and place them at the beginning of the array.
Sort them in non-decreasing order (increasing, with (if any) repetitions)
Sort them in strictly increasing order (no repetitions).
Print them onto the prime text file.
This is the final revision of my code (which works fine):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int prime(int n){ /*creates function of primality test*/
int c = 0;
if ( (n <= 0) || (n == 1) )
{
return 0;
}
else
{
for (int k = 2; k <= (int)sqrt((double)n); ++k)
{
if( n % k == 0)
{
c = 1;
}
}
if (c == 1)
{
return 0;
}
else
{
return 1;
}
}
}
int main(void)
{
int n;
printf("\nPlease enter a positive amount (less than 1,000) of integers you would like to scan for primality:\n");
for (;;)
{
scanf("%d", &n);
if (n>=1)
{
printf("\n");
break;
}
printf("\nError! Please enter a number between 1 and 1000:\n");
}
int random[n]; /*begins declaring array*/
srand(time(0)); /*seeds the srand function*/
for (int i = 0; i < n; ++i) /*fills in the array with random numbers from 2 to 1,000,000*/
{
random[i] = rand() % (1000000 - 2 + 1) +2;
FILE *fptr;
fptr = fopen("random_ints.txt","w"); /*writes the random numbers of the array onto a text file*/
for (int i = 0; i < n; ++i)
{
if (i == 0)
{
fprintf(fptr,"%d",random[i]);
}
else
{
fprintf(fptr,", %d",random[i]);
}
}
fclose(fptr);
if((n>0) && (n<3)) /*deals with case where we have only one or two random numbers, since more than two numbers are needed to work with our for loop in line 119*/
{
fptr = fopen("primes.txt","w"); /*checks whether the numbers are prime and, if so, writes them onto the prime text file*/
for (int i = 0; i <= n; ++i)
{
if (prime(random[i]) == 1)
{
if (i == 0)
{
fprintf(fptr,"%d",random[i]);
}
else
{
fprintf(fptr,", %d",random[i]);
}
}
}
fclose(fptr);
printf("Success!\n");
return 0;
}
else
{
if (n >= 3) /*deals with case where we have 3 or more random numbers*/
{
int p1 = 0; /*declares variable to be used to keep track of where to place a prime number once it is found on the array*/
for (int i = 0; i < n; ++i)
{
if (prime(random[i]) == 1) /*checks every value in the array for primes; if it finds one, saves it on the p1-th place and increases this value by one*/
{
random[p1] = random[i];
p1++;
}
}
for (int i = p1; i < n; ++i) /*(now, values from random[0] to random[p1 - 1] will be filled with random prime numbers) sets the rest of values to zero*/
{
random[i] = 0;
}
for (int i = 0; i < (p1-1); ++i) /*organizes the prime numbers in the array from least to greatest*/
{
for (int k = (i + 1); k < p1; ++k)
{
if (random[i] > random[k])
{
int temp = random[i];
random[i] = random[k];
random[k] = temp;
}
}
}
for (int i = 0; i < (p1 - 1); ++i) /*shifts the array to get rid of repeated primes (except for the last one)*/
{
if (random[i] == random[i + 1])
{
int m = (i + 1);
for (int k = (i + 1); k < p1; ++k)
{
if(random[i] != random[k])
{
random[m] = random[k];
m++;
}
}
}
}
int r = 0; int z = 0; /*records where the last non-repeated prime is located*/
while(random[z] != random[z + 1])
{
r++; z++;
}
fptr = fopen("primes.txt","w");
for (int i = 0; i < r; ++i)
{
if (prime(random[i]) == 1) /*verifies that the numbers on the array are prime then, if so, writes them onto the prime number text file*/
{
if (i == 0)
{
fprintf(fptr,"%d",random[i]);
}
else
{
fprintf(fptr,", %d",random[i]);
}
}
}
fclose(fptr);
printf("Success!\n");
return 0;
}
else{
return -1;
}
}
}