-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
132 lines (115 loc) · 3.49 KB
/
main.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
void printToFile(int sideSize, int numbersOrSpiral);
void printToConsole(int sideSize, int numbersOrSpiral);
void createArray(int sideSize);
bool checkForPrime(int number);
void primesSpiral(int sideSize);
#define MAX_SIDE_SIZE 301 //Can be modified (chosen for visibility in .txt file)
int array[MAX_SIDE_SIZE][MAX_SIDE_SIZE];
char spiral[MAX_SIDE_SIZE][MAX_SIDE_SIZE];
int main()
{
int sideSize;
printf("Enter side size for spiral (must be uneven, positive integer between 3 and 301) : ");
scanf("%d", &sideSize);
while((sideSize < 3) || (sideSize > MAX_SIDE_SIZE)){
printf("\nError. Please enter correct side size : ");
scanf("%d", &sideSize);
}
createArray(sideSize);
primesSpiral(sideSize);
printf("\n");
printToFile(sideSize, 0);
printf("The file UlamSpiral.txt has been created/updated.\n");
return 0;
}
void printToConsole(int sideSize, int numbersOrSpiral){
for(int i = 0; i < sideSize; i++){
for(int j = 0; j < sideSize; j++){
if(numbersOrSpiral == 0){
printf("%d\t", array[i][j]);
}else if(numbersOrSpiral == 1){
printf(" %c ", spiral[i][j]);
}
}
if(numbersOrSpiral == 0){
printf("\n\n\n");
}else if(numbersOrSpiral == 1){
printf("\n");
}
}
}
void printToFile(int sideSize, int numbersOrSpiral){
FILE *filePtr;
filePtr = fopen("UlamSpiral.txt", "w+");
for(int i = 0; i < sideSize; i++){
for(int j = 0; j < sideSize; j++){
if(numbersOrSpiral == 0){
fprintf(filePtr, "%d\t", array[i][j]);
}else if(numbersOrSpiral == 1){
fprintf(filePtr, " %c ", spiral[i][j]);
}
}
if(numbersOrSpiral == 0){
fprintf(filePtr, "\n\n\n");
}else if(numbersOrSpiral == 1){
fprintf(filePtr, "\n");
}
}
fclose(filePtr);
}
void createArray(int sideSize){
int rowIndex = 0;
int rowSize = sideSize;
int colIndex = 0;
int colSize = sideSize;
int n = sideSize * sideSize;
while((rowIndex < rowSize) && (colIndex < colSize)){
for(int i = (rowSize - 1); i >= rowIndex; i--){
array[rowSize - 1][i] = n--;
}
colSize--;
for(int j = (colSize - 1); j >= colIndex; j--){
array[j][colIndex] = n--;
}
rowSize--;
if((colIndex + 1) < colSize){
for(int k = rowIndex + 1; k <= rowSize; k++){
array[rowIndex][k] = n--;
}
}
rowIndex++;
if((rowIndex) < rowSize){
for(int l = rowIndex; l < colSize; l++){
array[l][colSize] = n--;
}
}
colIndex++;
}
}
bool checkForPrime(int number){
bool flag = false;
for(int i = 2; i <= (number/2); ++i){
if((number % i) == 0){
flag = true;
break;
}
}
return flag;
}
void primesSpiral(int sideSize){
for(int i = 0; i < sideSize; i++){
for(int j = 0; j < sideSize; j++){
if(array[i][j] != 1){
if(!checkForPrime(array[i][j])){
spiral[i][j] = 'x';
}else{
spiral[i][j] = '-';
}
}
}
}
}