This code here is is meant to grab random numbers (temperatures) and create a table with the corresponding hour in which the temperature was recorded. Here is an example of the output I am supposed to be receiving.
Temperature Conditions on October 9, 2015:
Time of Day Temperature in degrees F
0 85
1 80
2 97
3 90
4 68
5 75
6 77
7 98
8 97
9 62
etc...
Maximum Temperature for the day: <whatever> Degrees F
Minimum Temperature for the day: <whatever> Degrees F
Average Temperature for the day: <whatever.whatever> Degrees F
My problem is that when I run the code a dialog box appears and says the program has stopped working and I don't quite know why.
All the help will be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
int GetValue(int[]);
int main() {
int x, n, max = 0,min = 100, ArrayNumMax, ArrayNumMin, temperature[25];
float sum;
float average;
int num[25];
printf("Temperature Conditions on October 9, 2015:\nTime of Day \tTemperature in Degrees F\n");
for (x = 0; x <= 24; x++) {
//if statements to get min and max
temperature[x] = GetValue(temperature);
if (temperature[x] > max) {
max = temperature[x];
ArrayNumMax = x;
}
if (temperature[x] < min) {
min = temperature[x];
ArrayNumMin = x;
}
printf("\t%d\t\t\t\t\t%d\n", x,temperature[x]);
}
//prints statements
printf("\nMidnight\t\t\t\t%d\n\nMaximum Temperature for the day: %d Degrees F at %d\nMinimum Temperature for the day: %d Degrees F at %d\n", temperature[12],max,ArrayNumMax, min, ArrayNumMin);
//adds up all temps
sum=0;
for (x=0;x<25;x++){
sum=(sum+temperature[x]);
}
//prints and creates average
average=sum/25;
printf("Average Temperature for the day: %.2f Degrees F\n",average);
return 0;
}
//gets values and puts them into array
int GetValue(int value[]) {
int x, temp[x];
temp[x] = (rand()%(100-60+1))+60;
return temp[x];
}
1
What are you doing in your GetValue function?
int GetValue(int value[]) {
int x, temp[x]; // create an array of undeclared amount x....
temp[x] = (rand()%(100-60+1))+60; // at invalid memory set it to this value.
return temp[x]; // then return this memory that I have no control over
}
Scrap this and go with this...
void GetValue(int value[], int x) {
value[x] = (rand()%(100-60+1))+60;
}
Also above the main change
int GetValue(int[]);
to
void GetValue(int a[], int b);
Then in your main
//if statements to get min and max
GetValue(temperature, x);
if (temperature[x] > max) {
Also you should look into preprocessor macros.
Read about them here. http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm
such as #define
#define array_size 25
int array[array_size];
Then if you change your code and instead of 25 you need 50 then just change it once.
Two error :
x in GetValue is not initialized so temp[x] has cross the border , so program encounter a segmentation fault.
When you define temp[x], x's value is undefine ,and temp exceed program's stack.
The right one maybe like this:
int GetValue(int value[])
{
int x;
x=1;
int temp[x];
temp[x-1] = (int)((rand()%(100-60+1))+60);
return temp[x-1];
}
The result shows that :
Temperature Conditions on October 9, 2015:
Time of Day Temperature in Degrees F
0 65
1 96
2 60
3 83
4 67
5 79
6 66
7 92
8 83
9 77
10 87
11 66
12 77
13 66
14 68
15 82
16 74
17 79
18 63
19 73
20 86
21 70
22 80
23 81
24 80
Midnight 77
Maximum Temperature for the day: 96 Degrees F at 1
Minimum Temperature for the day: 60 Degrees F at 2
Average Temperature for the day: 76.00 Degrees F
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int num, lower, upper;
double squareroot;
int square;
int cube;
printf("enter your number:\n");
scanf_s("%d", &num);
do
{
printf("the lower value limit is ");
scanf_s("%d", &lower);
} while (lower < 0 || lower > 50);
do
{
printf("the upper value limit is ");
scanf_s("%d", &upper);
} while (upper < 0 || upper > 50);
// the formular to find the squareroot, square, cube
squareroot = sqrt(num);
square = num * num;
cube = num * num * num;
//a for loop
for (num = 0; num <= upper; num++) {
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
}
return 0;
}
i try to make a table to display the base number, square root, square, and cube
and set a limit for the table.
for example, if I input the lower number is 1 and the upper number is 5 then the table will stop at 5 then display the square root, square, and cube
At least this problem:
Mismatched specifiers/type
int square;
int cube;
...
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
Use "%d" with int, not "%ld".
Move assignments
Following assignments need to be inside the loop.
for (num = 0; num <= upper; num++) {
square = num * num;
cube = num * num * num;
Save time. Enable all compiler warnings
Try the Below Code Make all Changes I have added Comments to Clarify why I made The
#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int lower, upper;
double squareroot;
int square;
int cube;
//Read the Limits First
printf("Enter the Lower Limit: ");
scanf("%d", &lower);
printf("Enter the Upper Limit: ");
scanf("%d", &upper);
//Instead of Declaring an Entire Loop You Can Just Use an If Statement this Reduces Code Complexity
//You can also Set Your Limits
if(upper > 0 && upper < 50 && lower > 0 && lower < 50)
{
//THen Enter Actual Code
//Also Dont Set Lower to 0 It will Change Your Actual Value Instead Take another Loop Var
for (int i = lower; i <= upper; i++)
{
//Then Perform all Functions on i
squareroot = sqrt(i);
square = i * i;
cube = i * i * i;
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%d* || *%d*\n", i, squareroot, square, cube);
}
}
return 0;
}
Several issues:
You go to the trouble of asking for lower, but you don't use it to control your loop - your loop should befor( int num = lower; num <= upper; num++)
{
...
}
Put another way, if you always intend for your loop to start from zero, then you don't need lower at all.
You need compute the square root, square, and cube of num for each iteration of the loop. Instead, you're doing it once before the loop and just printing those same values over and over again;
You only need to print your table header once, outside the body of the loop;
You can compute your square root, square, and cube all as part of the printf statement:
printf( "%d %f %d %d\n", num, sqrt((double) num), num * num, num * num * num );
Field width specifiers are your friends - you can tell printf exactly how wide you want each column to be. Example:
printf( "%6d%12.2f%12d%12d", num, sqrt((double) num), num * num, num * num * num );
This means the column for num is 6 characters wide, the column for square root is 12 characters wide, with 3 characters reserved for the decimal point and two following digits, and the columns for square and cube are 12 characters wide. Example:
printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i++ )
printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );
Which gives output like this (lower == 1, upper == 10):
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
Full example:
#include <stdio.h>
#include <math.h>
int main( void )
{
int lower = 0, upper = 0;
printf( "Gimme a lower value: " );
while ( scanf( "%d", &lower ) != 1 || (lower < 0 || lower > 50 ))
{
/**
* Clear any non-numeric characters from the input stream
*/
while ( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "Gimme an upper value: " );
while ( scanf( "%d", &upper ) != 1 || (upper < lower || upper > 50 ))
{
while( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i++ )
printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );
return 0;
}
I've written the input section such that it will reject inputs like foo or a123. It will not properly handle inputs like 12w4, but that would make the example more complicated than it needs to be (you're not asking about input validation, you're asking about computation and formatting). Example run:
$ ./table
Gimme a lower value: foo
Nope, try again: a123
Nope, try again: 123
Nope, try again: 1
Gimme an upper value: 100
Nope, try again: 50
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
11 3.32 121 1331
12 3.46 144 1728
13 3.61 169 2197
14 3.74 196 2744
15 3.87 225 3375
16 4.00 256 4096
17 4.12 289 4913
18 4.24 324 5832
19 4.36 361 6859
20 4.47 400 8000
21 4.58 441 9261
22 4.69 484 10648
23 4.80 529 12167
24 4.90 576 13824
25 5.00 625 15625
26 5.10 676 17576
27 5.20 729 19683
28 5.29 784 21952
29 5.39 841 24389
30 5.48 900 27000
31 5.57 961 29791
32 5.66 1024 32768
33 5.74 1089 35937
34 5.83 1156 39304
35 5.92 1225 42875
36 6.00 1296 46656
37 6.08 1369 50653
38 6.16 1444 54872
39 6.24 1521 59319
40 6.32 1600 64000
41 6.40 1681 68921
42 6.48 1764 74088
43 6.56 1849 79507
44 6.63 1936 85184
45 6.71 2025 91125
46 6.78 2116 97336
47 6.86 2209 103823
48 6.93 2304 110592
49 7.00 2401 117649
50 7.07 2500 125000
I have created a program to search for prime numbers. It works without problems until the entered number is smaller than 52, when it is bigger output prints out some blank (0) numbers and I don't know why. Also other numbers have blank output.
My code is:
#include <stdio.h> //Prime numbers
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
int c[100], n, a[50], d, e, b = 1;
void sort() {
for (int i = 1; i < n; i++) {
if (c[i] > 1) {
a[b] = c[i];
printf("%d %d %d\n", a[1], b, i);
b++;
e = 2;
d = 0;
while (d <= n) {
d = c[i] * e;
c[d - 1] = 0;
e++;
}
}
}
}
int main() {
printf("Enter number as an limit:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
c[i] = i + 1;
}
sort();
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 1; i < b; i++) {
printf("%d ", a[i]);
}
return 0;
}
Here is output for 25:
Enter number as an limit:
25
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
But for 83 is:
Enter number as an limit:
83
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
2 10 28
2 11 30
2 12 36
2 13 40
2 14 42
2 15 46
2 16 52
0 17 58
0 18 60
0 19 66
0 20 70
0 21 72
0 22 78
0 23 82
Prime numbers between 1 and 83 are:
0 3 5 7 11 0 17 19 23 29 31 37 0 43 47 53 0 61 67 71 73 79 83
Blank spots always spots after 17th prime number. And always the blank numbers are the same. Can you help me please what is the problem?
The loop setting entries in c for multiples of c[i] runs too far: you should compute the next d before comparing against n:
for (d = c[i] * 2; d <= n; d += c[i]) {
c[d - 1] = 0;
}
As a matter of fact you could start at d = c[i] * c[i] because all lower multiples have already been seen during the previous iterations of the outer loop.
Also note that it is confusing to store i + 1 into c[i]: the code would be simpler with an array of booleans holding 1 for prime numbers and 0 for composite.
Here is a modified version:
#include <stdio.h>
int main() {
unsigned char c[101];
int a[50];
int n, b = 0;
printf("Enter number as a limit:\n");
if (scanf("%d", &n) != 1 || n < 0 || n > 100) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < n; i++) {
c[i] = 1;
}
for (int i = 2; i < n; i++) {
if (c[i] != 0) {
a[b] = i;
//printf("%d %d %d\n", a[0], b, i);
b++;
for (int d = i * i; d <= n; d += i) {
c[d] = 0;
}
}
}
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 0; i < b; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
chqrlie$ ./sieve4780
Enter number as a limit:
25
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
chqrlie$ ./sieve4780
Enter number as a limit:
83
Prime numbers between 1 and 83 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Your problem seems to be caused by the fact that you have declared an array with size 50, but in fact it goes further than that: imagine you want to use Eratosthenes' procedure to find the first 10,000 prime numbers. Does this mean that you need to declare an array of size 10,000 first (or even bigger), risking to blow up your memory?
No: best thing to do is to work with collections where you don't need to set the maximum size at declaration time, like a linked list, a vector, ..., like that you can make your list grow as much as you like during runtime.
today I have a problem with searching a max and min value in a column. I did it with a row, but with columns something went wrong. It's a task for university, so I have to operate using pointers. So I wrote a function:
int column_statistics(const int *ptr, int width, int height, int column_id, int* max, int* min, float *avg){
//problem generator
if(ptr==NULL || width<=0 || height<=0 || column_id<0 || max==NULL || min==NULL || avg==NULL || column_id>(--width)){
return 1;
}
int maximal;
int minimal;
int sum=0;
for(int i=0;i<width;i++){
printf("%d ",*(ptr+column_id*width+i));
sum+=*(ptr+column_id*width+i);
if(i==0){
minimal=*(ptr+column_id*width+i);
maximal=*(ptr+column_id*width+i);
continue;
}
if(*(ptr+column_id*width+i)<maximal && *(ptr+column_id*width+i)>minimal){
continue;
}
else if(*(ptr+column_id*width+i)>maximal){
maximal=*(ptr+column_id*width+i);
}
else if(*(ptr+column_id*width+i)<minimal){
minimal=*(ptr+column_id*width+i);
}
}
*max=maximal;
*min=minimal;
*avg=(float)sum/(float)height;
return 0;
}
I think that it is OK, but when I am testing this in main:
#include <stdio.h>
int column_statistics(const int *ptr, int width, int height, int column_id, int* max, int* min, float *avg);
int main(void) {
int input_array[3][5] = {{ 11, 81, -86, -63, 79}, { 94, -9, 11, -22, 72}, { 64, -61, 33, -36, -73}};
int *ptr = input_array[0];
int max, min, test;
float a;
for(int i=0; i<3; i++){
printf("%d %d %d %d %d\n", *(ptr+i*5),*((ptr+i*5)+1),*((ptr+i*5)+2),*((ptr+i*5)+3),*((ptr+i*5)+4));
}
puts("============================================");
for(int i=0;i<3;i++){
column_statistics(ptr, 5, 3, i, &max, &min, &a);
printf("Column %d: %d %d %.2f | Test: %d\n", ++i, max, min, a, test);
}
return 0;
}
Something goes wrong and in output I have something like this:
(The inputed 2d-array)
11 81 -86 -63 79
94 -9 11 -22 72
64 -61 33 -36 -73
(The actual output of a function, the first string are the numbers that were in the function, than there is a max and min from column, average of a column, and test from "problem generator")
11 81 -86 -63 Column 1: 81 -86 -19.00 | Test: 0
79 94 -9 11 Column 2: 94 -9 58.33 | Test: 0
-22 72 64 -61 Column 3: 72 -61 17.67 | Test: 0
So the problem is that function loads only 4 variable instead of 5, but I don't know if the problem is in main or in the column_statistics. Guys, if you see what is wrong please, report this. Have a nice day and thanks for your time.
The problem is most likely due to the condition column_id>(--width). That --width will modify the value of the variable. If you passed 5 for width, after --width the value will be 4.
That will affect your loops where you iterate using width, like for example the very first loop:
for(int i=0;i<width;i++){
It will also affect all calculation you have suing width, like for example *(ptr+column_id*width+i).
If you want to compare column_id with width - 1 then use width - 1 instead (like column_id>(width-1)).
I need to write a program which prints the conversion table from feet and inches to centimetres. The numbers printed in row i (counting from zero), column j (counting from zero) of the table should be the cm equivalent of i feet and j inches. i should go from 0 to 7, and j from 0 to 11. Each column should be five characters wide, and the cm figures should be rounded to the nearest integer.
The example of required output is given below:
0 3 5 8 10 13
30 33 36 38 41
61 64 66 69 71
91 94 97 99 102
The code I have prints only one row of inches and column of feet but I don't know how to make into table without producing lots of irrelevant repetitions.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int cm,p;
for (i=0; i<= 11; i++) {
cm =round(i * 2.54);
printf ("%5d",cm);
}
for (j=0; j<=7; j++) {
p =round(j* 12.0 * 2.54);
printf ("%5d\n",p);
}
return 0;
}
This produces:
0 3 5 8 10 13 15 18 20 23 25 28 0
30
61
91
122
152
183
213
What am I doing wrong?
You have one loop after the other. What you need to do is run through the inches loop every iteration of your feet loop. What you get is nested loops:
#include <stdio.h>
int main()
{
for (int feet = 0; feet <= 7; ++feet) {
for (int inches = 0; inches < 12; ++inches) {
int microns = (feet * 12 + inches) * 25400;
int rounded_cm = (microns + 5000) / 10000;
printf("%5d", rounded_cm);
}
puts("");
}
}
I've made some other changes in my version; you're encouraged to study it and understand why it does what it does (read the man page for puts(), for example). Don't just copy it and hand it in - it will be obvious it isn't your code.
An alternative approach is to use a single loop (in inches), and insert a newline when we reach the 11th inch in each foot:
#include <stdio.h>
int main()
{
for (int i = 0; i < 96; ++i) {
printf("%4d%s",
(i * 25400 + 5000) / 10000,
i%12==11 ? "\n" : " ");
}
}
(You'll want to give meaningful names to your constants; the above is written in a "code-golf" style).
Whatever you do, don't be tempted to avoid multiplying by instead adding 2.54 repeatedly in the loop. Floating-point numbers are not exact, and addition will accumulate the error.
OP needs to put the "inches" loop inside the "foot" loop as well answered by others. #Toby Speight #VHS
Code could do its "round to nearest" via the printf() statement by using "%5.0f" to control the output width and rounding.
Let code use foot/inch instead of i/j #KevinDTimm for clarity.
#include <stdio.h>
#define INCH_PER_FOOT 12
#define CM_PER_INCH 2.54
int main(void) {
// go from 0 to 7, and ...
for (int foot = 0; foot <= 7; foot++) {
// from 0 to 11
// for (int inch = 0; inch < INCH_PER_FOOT; inch++) { is more idiomatic
for (int inch = 0; inch <= 11; inch++) {
printf("%5.0f", (foot * INCH_PER_FOOT + inch) * CM_PER_INCH);
}
puts("");
}
}
Output
0 3 5 8 10 13 15 18 20 23 25 28
...
213 216 218 221 224 226 229 231 234 236 239 241
You are running your loops backwards. First you need to run through feet and then through inches. But you are having it the other way round. Check the following snipped and compare it with your code and try to understand what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // for rounding of a number
int main()
{
int i,j;
int cm,p;
for(i=0; i<=7;i++) {
for(j=0;j<=11;j++) {
cm = round(i*30.48 + j*2.54);
printf ("%5d",cm);
}
printf("\n");
}
return 0;
}
I'm trying to submit the solution for Spoj - Prime Intervals problem. But I'm getting a runtime error SIGXFSZ. It is given that, it occurs due to exceeded file size. I have used the Sieve of Eratosthenes algorithm to find the prime numbers. I don't understand what's wrong with my code and this is bugging me from last the 2 days. Please help me with the submission. Here is my code...
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
int main(){
int t, turn;
long i, l,u,k,j;
scanf("%d", &t);
/*Looping for t test cases*/
for(turn=0; turn<t; turn++){
scanf("%ld %ld", &l, &u);
bool arr[u-l+1];
/*Assigning whole array with true*/
memset(arr, true, u-l+1);
/*Sieve of Eratosthenes logic for assigning false to composite values*/
for(i=0; i<=(int)sqrt(u)-l; i++){
k=0;
j = i+l;
if(arr[i]==true){
while((j*j + k*j) <= u){
arr[(j*j + k*j) - l] = false;
k++;
}
}
}
/*Printing all the primes in the interval*/
for(i=0; i<u-l; i++){
if(arr[i]==true){
printf("%ld\n", i+l);
}
}
}
return 0;
}
Test Input:
2
2 10
2 100
Output:
2
3
5
7
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
I ran the posted code. the results were far from correct.
Most of the numbers output are not primes and fails to check the last number is the range, as shown in the second set of results
Here are the results:
1 <-- 1 test case
20 100 <-- range 20...100
20 <-- the outputs
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
Note: using 1 as the low end of the range usually results with no output produced
here is another run
The output should have been 5 7 11
1 <-- test cases
5 11 <-- range
5 <-- outputs
6
7
8
9
10
The following code does not try to minimize the size of the arr[] array, and if the upper end of the range is less than 16k then could declare the arr[] as short rather than unsigned int
The lowest valid value for the low end of the input is 2, but the code is not checking for that low limit, you might want to add that check.
The code makes no effort to minimize the number of loops executed by checking for the square root of the upper limit, you might want to add that check.
The code compiles cleanly, handles the case when the upper limit is a prime and when the lower limit is a prime as well as when the limit values are not primes.
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int numTestCases, testCase;
size_t i; // index
size_t lowLimit;
size_t upperLimit;
size_t k; // offset multiplier
scanf("%d", &numTestCases);
/*Looping for t test cases*/
for(testCase=0; testCase<numTestCases; testCase++)
{
scanf("%lu %lu", (unsigned long*)&lowLimit, (unsigned long*)&upperLimit);
unsigned arr[upperLimit+1];
/*Assigning whole array to indicate entry is a prime*/
memset(arr, 0x01, upperLimit+1);
/*Sieve of Eratosthenes logic for assigning false to composite values*/
//size_t sqrtUpperLimit = (size_t)ceil(sqrt(upperLimit));
for(i=2; i<= upperLimit; i++)
{
if(arr[i])
{
if( i >= lowLimit )
{
printf("%ld\n", i);
}
for( k=2; (i*k) <= upperLimit; k++)
{
arr[(i*k)] = 0;
}
}
}
}
return 0;
} // end function; main
here is an edited version of the code, with the addition of some instrumentation in the way of prompts to the user via calls to printf()
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int numTestCases, testCase;
size_t i; // index
size_t lowLimit;
size_t upperLimit;
size_t k; // offset multiplier
printf("enter number of test cases\n");
scanf("%d", &numTestCases);
/*Looping for t test cases*/
for(testCase=0; testCase<numTestCases; testCase++)
{
printf( "enter lower limit upper limit limits\n");
scanf("%lu %lu", (unsigned long*)&lowLimit, (unsigned long*)&upperLimit);
unsigned arr[upperLimit+1];
/*Assigning whole array to indicate entry is a prime*/
memset(arr, 0x01, upperLimit+1);
/*Sieve of Eratosthenes logic for assigning false to composite values*/
//size_t sqrtUpperLimit = (size_t)ceil(sqrt(upperLimit));
for(i=2; i<= upperLimit; i++)
{
if(arr[i])
{
if( i >= lowLimit )
{
printf("%ld\n", i);
}
for( k=2; (i*k) <= upperLimit; k++)
{
arr[(i*k)] = 0;
}
}
}
}
return 0;
} // end function; main
Using the above instrumented code and the input of:
5 2 3 30 31 20 27 2 3 4 5
it worked perfectly.
This was the output:
enter number of test cases
5
enter upper/lower limits
2 3
sizeof arr[]: 4
2
3
enter upper/lower limits
30 31
sizeof arr[]: 32
31
enter upper/lower limits
20 27
sizeof arr[]: 28
23
enter upper/lower limits
2 3
sizeof arr[]: 4
2
3
enter upper/lower limits
4 5
sizeof arr[]: 6
5