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 8 years ago.
Improve this question
Good day , i'm having this issue with the following C code . I'm trying to perform this operation, the sum of (each entered number has to be multiplied by its generated one) . What am I missing ?
i'm getting this error : incompatible types when assigning to type 'float *' from type 'float' .
Any help will be welcomed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
int main(int argc, char *argv[])
{
application();
return 0;
}
void application(void)
{
int input = 0;
float number[10];
float total =0;
float *ptr;
float generatedNum;
srand(time(NULL));
for(input; input<11; input++)
{
generatedNum = 2 *(float)rand()/(float)RAND_MAX - 1;
printf("\n\n\t\tEnter number %d : ", input);
scanf("%f", &number[input]);
ptr = number * generatedNum;
printf("\n\t\t\t\t\tMachine Value: %.1f", generatedNum);
}
for(input; input<11; input++)
{
total += *ptr;
ptr++;
}
printf("\n\n\n\t\tTHE NEURON IS: %.2f", total);
}
An easier solution is to simply keep everything in one loop. There really isn't a need for a pointer in this situation as nothing requires them, add the values as you get them. Also you never use the generatedNum (although maybe this is for further development??)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void application(void)
{
int input = 0;
float number[10];
float total =0;
float generatedNum;
srand(time(NULL));
for(; input<10; input++)
{
generatedNum = 2 *(float)rand()/(float)RAND_MAX - 1;
printf("\n\n\t\tEnter number %d : ", input);
scanf("%f", &number[input]);
total += number[input];
printf("\n\t\t\t\t\tMachine Value: %.1f", generatedNum);
}
printf("\n\n\n\t\tTHE NEURON IS: %.2f", total);
return;
}
int main(int argc, char *argv[])
{
application();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void application(void)
{
int input = 0;
float number[10];
float total =0;
float *ptr;
float generatedNum;
srand(time(NULL));
for(; input<10; input++)
{
generatedNum = 2 *(float)rand()/(float)RAND_MAX - 1;
printf("\n\n\t\tEnter number %d : ", input);
scanf("%f", &number[input]);
ptr = number;
printf("\n\t\t\t\t\tMachine Value: %.1f", generatedNum);
}
for(input = 0; input<10; input++)
{
total += *ptr;
ptr++;
}
printf("\n\n\n\t\tTHE NEURON IS: %.2f", total);
}
int main(int argc, char *argv[])
{
application();
return 0;
}
your array will get out of bound if you go for < 11 because it might contain 10 position but it's starting at 0.
Also the first loop manipulates your input variable which is then higher than the required condition of your second loop.
So you need to set it back to 0.
Working example!
Related
#include <stdio.h>
#include <stdlib.h>
#include "simpio.h"
#include "stdio.h"
int main()
{
float answer;
int D;
int N;
int i=0;
int p=1;
printf("How much :");
N=GetInteger();
for (i=0; i!=N; i++)
{
for(p=1; p!=N; p++){
D=1/p;
answer+=D;
}
}
printf("the answer is: %.2f",apotelesma);
return 0;
}
for example if I gave N=100 then the program was suppose to
1/1+1/2+1/3....1/N
and then give me the 5.19
but for some reason its just skips it
I know it's an easy question I started programing like for two weeks ma trying to learn alone.
Replace the inner-loop with this to force floating-point math:
for(p=1; p!=N; p++)
answer += 1.0/p;
Also initialise float answer = 0;.
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 4 months ago.
I'm trying to build a word guesser where you guess a word from the given choice. The words are stored in an array. The player is losing even though they must win.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guess(int N) {
int number, guess, numberofguess = 0;
int myval[] = {"R22", "B3", "R33", "B232", "R12",
"B45", "R2", "B12", "R45", "B32"};
srand(time(NULL));
number = rand() % N;
printf("choose a number "
"{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");
char str[20];
gets(str);
printf("your guess is %s\n", str);
if (str == myval[number]) {
printf("you win\n");
} else
printf("you lose\n");
printf("the number is %s", myval[number]);
}
main() {
int N = 9;
guess(N);
return 0;
}
I want to make the code in such a way when the player enters the code and it matches with the random output the if statement works.
Use strcmp while comparing the strings in C.
When creating an array from strings, you should create it as char*.
Using gets is not good practice
You should specify the return type of all functions, including main(), which should return an int.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void guess(int N) {
int number, guess, numberofguess = 0;
const char* myval[] = {"R22", "B3", "R33", "B232", "R12",
"B45", "R2", "B12", "R45", "B32"};
srand(time(NULL));
number = rand() % N;
printf("choose a number "
"{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");
char str[20];
gets(str);
printf("your guess is %s\n", str);
if (strcmp(str,myval[number]) == 0) {
printf("you win\n");
} else
printf("you lose\n");
printf("the number is %s", myval[number]);
}
int main() {
int N = 9;
guess(N);
return 0;
}
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
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 1 year ago.
Improve this question
Please tell me what is wrong in my code.
ps- i know about floor and ceiling functions but i came up with this code by myself and would like to know, what should i change.
#include <stdio.h>
#include <math.h>
int main()
{ int q,start,end;
int i,j,k,count=0;
scanf("%d",&q);
while(q--)
{
for (i=1;i<=q;i++)
{
scanf("%d %d",&start,&end);
}
for(j=start ;j<=end;j++)
{
for(k=1;k*k<=j;k++)
{
if(k*k == j)
count=count+1;
}
}
printf("%d \n",count);
}
}
I optimised your code. This should run in O(q*n^(1/2))
#include <stdio.h>
#include <math.h>
int main()
{ int q,start,end;
int i,j,k,count=0;
scanf("%d",&q);
while(q--)
{
/* remove extra loop */
scanf("%d %d",&start,&end);
/* initialize count */
count=0;
for(int j=1 ;j*j<=end;j++)
{
if(j*j>=start && j*j<=end)
count=count+1;
}
printf("%d \n",count);
}
return 0;
}
Further more, optimization leads to O(q*n^(1/2)):
#include <stdio.h>
#include <math.h>
int main()
{ int q,start,end;
int i,j,k,count=0;
scanf("%d",&q);
while(q--)
{
/* remove extra loop */
scanf("%d %d",&start,&end);
/* initialize count */
count=floor(sqrt(end)) - ceil(sqrt(start)) + 1;
printf("%d \n",count);
}
return 0;
}
Source
You will have to read only one pair, not q pairs, for each iteration.
You have to initialize count` to zero at the beginning of each iteration, not only at the beginning of the program.
include <stdio.h>
#include <math.h>
int main()
{ int q,start,end;
int i,j,k,count=0;
scanf("%d",&q);
while(q--)
{
/* remove extra loop */
scanf("%d %d",&start,&end);
/* initialize count */
count=0;
for(j=start ;j<=end;j++)
{
for(k=1;k*k<=j;k++)
{
if(k*k == j)
count=count+1;
}
}
printf("%d \n",count);
}
}
Also note that you can obtain the count with only one loop like this:
count=0;
for(j=0 ;j*j<=end;j++)
{
if(start <= j*j)
count=count+1;
}
So I'm really new to programming. I need to write a program that, if I give it any array of integers, it'll be able to find the two numbers closest to each other, and then give the difference between those two numbers. Also, the first number must be the number of integers that are going to be in the array.
So for example, I give it 3 1 4 8. The first 3 means that there will be three integers, so it must find the closest two numbers between those three. In this case, it's 4 - 1 = 3, so the output should be 3, but when I write it it gives me 16.
This is what I have, and I don't know what's wrong:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, str[n*n], minimum, c;
/* here I'll make a new array, and its elements will be all the
differences between all the elements of the previous one */
for(a=0;a<n;a++)
for(b=0+a*n;b<n;b++) {
if(st[b-a*n]==st[a])
str[b]=32000;
else
str[b]=abs(st[b-a*n]-st[a]);
}
// here I'll find the smallest element on the last made array
minimum = str[0];
for(c=0;c<n*n;c++)
{
if(str[c]<minimum);
{
minimum=str[c];
}
}
printf("%d", minimum);
return 0;
}
Edit: I tried to fix it with your answers but it still doesn't work.
New code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = st[0];
for(a=0;a<n;a++)
for(b=0;b<n;b++) {
if((st[b] != st[a]) && (abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}
Edit 2: Ok, I fixed it now. Thanks a lot ^^
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int n, i;
printf("write numbers here\n");
scanf("%d", &n);
int st[n];
for(i=0;i<n;i++)
scanf("%d",&st[i]);
int a, b, minimum;
minimum = INT_MAX;
for(a=0;a<n;a++)
for(b=a+1;b<n;b++) {
if((abs(st[b]-st[a]))<minimum)
minimum = abs(st[b]-st[a]);
}
printf("%d", minimum);
return 0;
}