Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
So i'm trying to solve this codeforce problem https://codeforces.com/contest/431/problem/A
.Basically i input 4 integers(a[0]...a[3]) and an array of integers between 1 and 4 then i need to output the sum of the string values according to the 4 initial integers.(check the codeforce's exemples)
So my code did work on the 5 initial tests but i had a wrong output on the 6th test
enter image description here
Here's the code
#include <stdio.h>
int main()
{
int test=1;
long s,result=0;
long a[3];
int i,x;
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
if (a[i]==0)
test=0;
}
scanf("%d",&s);
while (s!=0)
{
if (test==0)
break;
x=s%10;
s=s/10;
result=a[x-1]+result;
}
printf("%d",result);
return 0;
}
Your help would be much appreciated.
There have several problems with your code. But the most severe problem for which you are getting wrong output because you are using "%d" format specifier for long values, but its "%ld" actually. Using %ld will solve the problem.
Leaving rest of the problems for you to find out. Happy coding!
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
This post was edited and submitted for review 6 days ago.
Improve this question
I know there are standard solution source codes for this problem. I'll look at them once im done trying it my way. pls help me get my way right.
I've attached the source code ive perfected over two days.
the output is correct except for some 1's everytime no. of digits changes(of the natural numbers being checked for being armstrong).
Pls run the source code urself and see. i hope the code and its purpose is understandable.
gist: i know i can get the solution to this problem, i just want to know what's wrong with the code ive whipped up.
/*for finding armstromg numbers til n-digit numbers. */
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
int num;
int rem=1,sum,temp;//variables of armstrong checking loop.
int dig_pow;//isolated digits raise to the power
printf("Enter the number of digits you want the last armstrong number to be:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
int lim=pow(10,i+1);//number next to the last number in the digit range requested.
for(j=1;j<lim;j++)
{
sum=0;
temp=j;
num=j;
while(num>0)
{
rem=num%10;
sum=sum+pow(rem,i);
num/=10;
}
if(sum==temp)
{
printf("%d, ",sum);
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have an ASCII string coming from UART that looks something like "43.533a,5532" and I'd like to extract the two numbers. My idea was to separate them using strtok with the comma as delimiter and then remove the last character from the first string and afterwards convert the numbers using atoi() or is there an easier way with sscanf()? String manipulation is nothing I'm regularly using.
Another problem is, if the String looks different, how could I catch that beforehand?
Yes you can do this easily with sscanf().
Following is an example. See it working here:
#include <stdio.h>
int main(void) {
float a;
int b;
char *sNum = "43.533a,5532";
sscanf(sNum, "%fa,%d", &a, &b);
printf("a= %f || b= %d", a,b);
return 0;
}
Output:
a= 43.533001 || b= 5532
Note: Since float is having precision to 6 decimal place by default, so you may need to consider it and correct it if necessary.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define size 20000000
int prim[size];
int i, zahl, zaehler, erg;
int sieve(int zahl, int prim[], int zaehler) {
if(zahl == 2000000)
return 1;
for(i=0; i<=zaehler; i++) {
erg = zahl%prim[i];
if(erg==0) {
zahl++;
return sieve(zahl, prim, zaehler);
}
}
zaehler++;
prim[zaehler]=zahl;
zahl++;
printf("%d\n", prim[zaehler]);
return sieve(zahl, prim, zaehler);
}
int main(){
zaehler = 0;
zahl = 2;
for(i=0;i<size;i++)
prim[i]=2;
sieve(zahl, prim, zaehler);
}
When trying to calculate prime numbers, when i run this code, it always crushes at the number 64901.
What might be the problem?
Ironically, this is literally a stack overflow due to recursion. You can make your stack large (which will only delay the issue), or change from a recursive solution to an iterative one.
(and for what it's worth, some debuggers won't be able to help you in this situation. And it's very difficult to beginners in C to understand what is going wrong until the first time they hit this problem. So congrats! You're leveling up in C)
A cheap way to verify it's indeed a stack overflow is to create extra memory on your stack in the recursive function and see if the number it crashes on changes from 64901. My guess is if you put like char dummy[2048] in there, it will crash much sooner.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am starting to learn C. Today I was trying a little program that just do a average point starting from 3 input.
After all I wanted to print the number of the averages done in the session, so I insert a simple
counter=counter+1;
into the main while loop and a
printf("you done the average %d times", counter);
before the return 0.
The problem is: if I do the average for just 1 or 2 times, the counter show
every time a different number, never the right, but ever around the int maximum. I tried everything, but it don't work. Where is my mistakes?
This is my first post on this site, i read the rules but i'm sorry if i'm breaking just one. The variable "counter" is declared.
int main()
{
int vote1, vote2, vote3, tot, media, contatore, err;
char opz;
do{
after this, i start an while loop, and this is its end:
contatore=contatore+1;
} while(opz!='n');
printf("hai eseguito la media %d volte", contatore);
return 0;
obviously the code is in italian, where counter=contatore
You have to initialise the variable:
int contatore = 0;