deletion of first two element of array declared statically not working - c

I am under a situation in a c that suppose i have code below:
#include <stdio.h>
main()
{
int size=8;
int data[18]={12,9,1,7,4,5,3,11};
int i,newS;
printf("check1 \n");
newS= size+1;
data[newS]=data[0] +data[1];
printf("news %d \n",data[newS]);
printf("check2 \n");
for(i=0;i<21;i++)
{
if (data[i] <data[newS] )
{
printf("check3 \n");
data[newS+1]= data[newS] +data[i] ;
newS++;
}
else
{
printf("check4 \n");
}
}
for(i=0;i<21;i++)
{
printf("%d ",data[i]);
}
printf("\n");
}
I expect it to produce result like this : 12 9 1 7 4 5 3 11 21 33 42 43 50 54 59 62 73 18 18 8. But i don't know why it has one "0" just after "11".How to remove this zero ?
The output obtained by corresponding code is this (which is not expected):
12 9 1 7 4 5 3 11 0 21 33 42 43 50 54 59 62 73 18 18 8

Here is the solution i have made to my problem of removing zero : (below is the code for future reference of any user).
#include <stdio.h>
main()
{
int size=8;
int data[18]={12,9,1,7,4,5,3,11};
int i,newS;
printf("check1 \n");
newS= size;
data[newS]=data[0] +data[1];
printf("news %d \n",data[newS]);
printf("check2 \n");
for(i=0;i<21;i++)
{
if (data[i] <data[newS] )
{
printf("check3 \n");
data[newS+1]= data[newS] +data[i] ;
//break;
newS++;
}
else
{
printf("check4 \n");
}
}
for(i=0;i<21;i++)
{
printf("%d ",data[i]);
}
printf("\n");
}

1) The topic of the question keeps changing. This is needlessly frustrating. If you have two questions, either express them both or break them into two separate posts.
2) Hint: What's the index of the last element of an 8-value array?
3) For delection -- restoring my original answer -- shift all the higher-indexed values downward one space in the array, and decrease the length by one.

Related

Eratosthenes prime numbers

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.

Why am I getting random numbers in my result?

I am trying to 4x4 input in an 5x5 array and get the sum of each lines on the fifth lines.
I'm sure if you read my code below, you'll know what I am trying to talk about.
For example if I type in:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The expected Result should be:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
13 14 15 16 58
28 32 36 40 136
Instead, I am getting a result like:
1 2 3 4 10
5 6 7 8 32792
9 10 11 12 42
13 14 15 16 58
28 32 36 40 -501277720
I thought about why I get these random values, but couldn't find a solution. Why am I getting these values and what can I do to solve it?
#include <stdio.h>
int main ()
{
int gradeArr[5][5];
int i,j;
printf("Input grades:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&gradeArr[i][j]);
gradeArr[i][4] += gradeArr[i][j];
}
}
printf("%d\n", gradeArr[1][4]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
gradeArr[4][i] += gradeArr[j][i];
}
gradeArr[4][4] += gradeArr[4][i];
}
printf("Result: \n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",gradeArr[i][j]);
}
printf("\n");
}
return 0;
}
You need to initialize the array.
Try
int gradeArr[5][5] = {0};

How to generate tables using arrays?

#include <stdio.h>
#include <stdlib.h>
int main()
{
system("color f0");
int k,i,j,n;
printf("Generate tables upto:");
scanf("%d",&n);
int tables[n][10];
printf("Table\t");
for(k=1;k<=10;k++)
{
printf("%dx\t",k);
}
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
tables[i][j]=i*j;
printf("%d\t",tables[i][j]);
}
}
return 0;
}
This is my code which i am working on but unfortunately I am not able to generate it the way I want.
The required output should look like this.
proposal fix for your code
C-arrays start at 0
you're missing tabs and linefeed at the proper locations
code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("color f0");
int k,i,j,n;
printf("Generate tables upto:");
scanf("%d",&n);
int tables[n][10];
printf("Table\t");
for(k=1;k<=10;k++)
{
printf("%dx\t",k);
}
printf("\n");
for(i=2;i<=n;i++)
{
printf("%d\t",i);
for(j=1;j<=10;j++)
{
tables[i-1][j-1]=i*j;
printf("%d\t",tables[i-1][j-1]);
}
printf("\n");
}
return 0;
}
display with n=4
Generate tables upto:4
Table 1x 2x 3x 4x 5x 6x 7x 8x 9x 10x
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
Array indexing starts from 0 and goes up to n-1. So you are accessing out of bounds which is undefined behaviour.
So you need to rewrite the loops as:
for(i=0; i < n; i++) {
for(j=0; j < 10; j++) {
tables[i][j] = (i+1)*(j+1);
printf("%d\t", tables[i][j]);
}
}

SIGXFSZ runtime error

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

Printing numbers

I am writing a program to display as below
when n=3
1 2 3
7 8 9
4 5 6
when n=5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
my program is
#include<stdio.h>
#include<conio.h>
int main()
{
int n=5,r=1,c=1,i=1,mid=0;
if(n%2==0)
mid=(n/2);
else
mid=(n/2)+1;
printf("mid = %d\n",mid);
while(r<=n)
{
while(c<=n)
{
printf("%d ",i);
c++;
i++;
}
r++;
if(r<=mid)
i=i+n;
else
i=i-(2*n);
printf("\n");
c=1;
}
getch();
return 0;
}
when I give n=3, I am getting my expected output. but when I give n=5 I am getting as below
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
11 12 13 14 15
Could someone please help how to achieve expected output.
Using you code the solution is
#include<stdio.h>
#include<conio.h>
int main()
{
int n=5,r=1,c=1,i=1,mid=0;
int maxRow = n;
if(n%2==0){
mid=(n/2);
maxRow--;
}
else
mid=(n/2)+1;
printf("mid = %d\n",mid);
while(r<=maxRow)
{
while(c<=n)
{
printf("%d ",i);
c++;
i++;
}
r++;
if(r<=mid)
i=i+n;
else if (r >= n)
i=n+1;
else
i=i-((1+(r-mid))*n);
printf("\n");
c=1;
}
getch();
return 0;
}
As you can see:
the i=i-(2*n); is changed. What you wrote wasn't generic, but specific for the n=3 case.
I added else if (r >= n).
Last thing you must use a specific variable for the outer while because of n must be decremented if n is even.
Some tips:
Give to your variables explanatory names
To make your code more readable declare variables 1 per line, if you want to init them.
Live empty lines between code chunks.
int main ()
{
int squareDim=5;
int row=1;
int col=1;
int valueToPrint=1;
int mid=0;
int maxRow = squareDim;
if(squareDim%2==0)
{
mid=(squareDim/2);
maxRow--;
}
else
{
mid=(squareDim/2)+1;
}
printf("mid = %d\n",mid);
while(row<=maxRow)
{
while(col<=squareDim)
{
printf("%d ",valueToPrint);
col++;
valueToPrint++;
}
row++;
if(row<=mid)
{
valueToPrint=valueToPrint+squareDim;
}
else if (row >= squareDim)
{
valueToPrint=squareDim+1;
}
else
{
valueToPrint=valueToPrint-((1+(row-mid))*squareDim);
}
printf("\n");
col=1;
}
return 0;
}

Resources