Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is part of a code I am writing as an assigment in C.
What I am trying to do is to detect negative numbers in an array and fill another array with these negative numbers. Meanwhile, I'd like to count those negative numbers.
All of this sounds pretty easy and everything seems to work out until I try to print the number of negative elements. This is what I get:
6422204 numbers are negative.
I simply cannot figure out what is wrong, or even if this is the good way to do it.
Here's the code:
double arr[10] = {21, -3.4, 65, 7.6, -12, 66, 10, -2, 5, 135};
int length = sizeof(arr)/sizeof(double);
printf("There are %d numbers in the array.\n",length);
for(int i = 0; i < 10; i++)
{
printf("[%d]=%.1lf ", i, arr[i]);
}
int neg = 0;
double negative[10];
for(int j = 0; j < 10; j++)
{
if(arr[j] < 0)
{
negative[j] = arr[j];
neg++;
}
}
printf("\n\n%d numbers are negative.", &neg);
Thanks for the answer!
Remove & in printf:
printf("\n\n%d numbers are negative.", neg);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to C and what I'm trying to do is ask the user to input 10 numbers in which it will then find the largest one. However, sometimes it just prints the last number inputted as the largest and I'm not sure where I'm going wrong.
void find_largest()
{
int numbers[10];
for (int i = 0; i < 10; ++i)
{
printf("Enter a number: ");
scanf("%d", &numbers[i]);
}
int largest = numbers[0];
for (int i = 1; i < 10; ++i)
{
if (numbers[i] > numbers[0])
{
largest = numbers[i];
}
}
printf("%d\n", largest);
}
Basically, you assigned the value of largest element is array's first element. But after this operation you should check the largest element with all of the array's number. If the one of the element in the array is larger then the largest variable, you should assign this value to the largest variable until checking the last element.
So you should change numbers[i]>largest
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#define N 500
int subscriptSquares[N];
void loadArray(int nums[], int size)
{
for(int i = 0; i <= (size - 1); i++)
{
nums[i] = i * i;
printf("%i\n", nums[i]);
}
}
What I'm trying to do more specifically is load an array of size 500 with its subscript squares (so subscriptSquares = {0, 1, 4, 9, 16, 25, ...250000}). My problem is somewhere along the line the values are shortened. The last value in the array actually becomes 249001 instead of 250000 and so on so forth. I thought it had something to with the data type so I switched it to double but ended up with same values with the obvious annoyance of decimals. int subscriptSquares[N] is inside the main function. If you need more information let me know
In your code you have defined an array of size 500. It will contain indices from 0 to 499.
So, the last result of your calculation will be 499*499 = 249001.
If you want to get the result of 500*500 then you have to declare an array of 501 locations. The below code shows how.
#define N 501
int subscriptSquares[N];
void loadArray(int nums[], int size)
{
for(int i = 0; i <= (size - 1); i++)
{
nums[i] = i * i;
printf("%i\n", nums[i]);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am following a introductory C programming course and the first assignment is to find number of perfect squares in a given range.
I am trying to get the first perfect square root, but when I try to assign the first square root to a variable, i am unable to do so and it always shows 0.
This is the program that I have written:
#include<stdio.h>
void main()
{
float y= 0;
float k = 1.0;
float n;
int i=0;
int first_sqrt;
first_sqrt = 0;
printf("enter number: \n");
scanf("%f",&n);
// finding the first perfect square
for(y = 0; y<=10000; y++)
{
while((k*k - n)>0.0001 || (n - k*k)> 0.0001)
{
k = (k + n/k) / 2;
//printf("%f\n", k);
}
i = (int)k;
if(i*i == n)
{
printf("perfect squareroot: %d\n", i);
i = first_sqrt;
y = 10001;
//break;
}
else
{
printf("not perfect square: %f\n", n);
n = n+1;
}
}
printf("first perfect square root: %d\n", first_sqrt);
}
I am sorry for posting the whole program, but I have no idea where the problem might be. This is the first assignment of the first week so I don't have understanding of a lot functions in C yet and I can't use math function for this assignment.
Any help would be appreciated. Have been searching all day about this but couldn't understand much.
A basic direction towards the problem would be most appreciated. Thanks.
The expression i = first_sqrt; assigns first_sqrtto i and not i to first_sqrt. Change it to first_sqrt = i;. Apart from this you can remove the comment from //break;.
Reverse this line
i = first_sqrt;
to first_sqrt = i;
You mixed up an assignment. This:
i = first_sqrt;
Should be:
first_sqrt = i;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! The input begins with the number t of test cases in a single line (t <= 10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n - m<=100000) separated by a space.
I don't know how to solve the problem with advanced concepts so I solved it by using just loops.
The time limit to this problem is 6.00s
#include <stdio.h>
int main(void)
{
int a[1],b[1],j,i,test,k,flag;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
for(k=0;k<1;k++)
{
scanf("%d %d",&a[k],&b[k]);
}
for(j=a[0];j<=b[0];++j)
{
flag=0;
for(k=2;k<j;++k)
{
if(j%k==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d",j);
}
}
}
return 0;
}
Couple of suggestions that will improve performance.
You don't need to check all the way to b[0]. You need to check only up to sqrt(b[0]).
Update the loop so that you check only odd numbers not all numbers.
Replace
for(j=a[0];j<=b[0];++j)
{
by
int stop = sqrt(b[0]);
// Start with an odd number and keep incrementing j by 2 to keep it that way
for(j= (a[0]/2)*2+1; j <= stop; j +=2 )
{