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 5 years ago.
Improve this question
int main() {
long int a;
long int b;
long int c;
long int d;
long int e;
scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e);
int i,j;
long int sum = 0;
long int largestsum =0;
long int smallestsum = 0 ;
long int a [5] = {a,b,c,d,e};
for ( i =0;i<5;i++){
for (j = 0;j<5;j++){
if (a[j]!=a[i]){
sum+=a[j];
}
}
if (largestsum <sum){
largestsum = sum;
}
if (smallestsum>sum){
smallestsum = sum;
}
}
printf("%ld %ld",largestsum,smallestsum);
return 0;
}
i'm trying to find the largest and the smallest sums between 5 inputs the problem is that i made the array of long ints the same as variables and i have the error conflicting data types in a which is the array what is the problem ?
What is a ?
Is it a long int as in line 2 ? long int a;
Or is it an array of long int as in line 12 long int a [5] = {a,b,c,d,e};
The compiler gets confused when he sees line 12, assumes you are doing the same thing again (which it would probably tolerate), then sees that you are using a different type (array instead of long int).
He concludes "Those are not the same types. That is a conflict of types."
Solution:
Rename a-the-array to "liArray", everywhere it is referenced.
Et voila: gcc -Wall Toy.c does not complain and running it does not crash.
Afterwards read the comments, they have more to say on how to get the program actually do what it is supposed to. Below I did only the array renaming.
#include <stdio.h>
int main()
{
long int a;
long int b;
long int c;
long int d;
long int e;
scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e);
int i,j;
long int sum = 0;
long int largestsum =0;
long int smallestsum = 0 ;
long int liArray[5] = {a,b,c,d,e};
for ( i =0;i<5;i++){
for (j = 0;j<5;j++){
if (liArray[j]!=liArray[i]){
sum+=liArray[j];
}
}
if (largestsum <sum){
largestsum = sum;
}
if (smallestsum>sum){
smallestsum = sum;
}
}
printf("%ld %ld",largestsum,smallestsum);
return 0;
}
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 2 years ago.
Improve this question
I am not able to understand where I am going wrong. Please help! I am new to the website. Appreciate all the help. Thanks a lot :D
#include <stdio.h>
int main()
{
printf("Hello World")
}
int factorial(int x) {
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int a = 9;
int b;
b = factorial(int a);
printf("%i", b);
I have corrected the code and added some comments. I also rearranged the factorial slightly, so that it works for 0! which is 1.
#include <stdio.h>
int factorial(int x) { // added the argument type int
int product = 1; // use another variable
for(int i = 2; i <= x; i++) {
product *= i;
}
return product;
}
int main()
{
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
Note that you can only generate up to 12! and after that you get overflow due to the range of a 32-bit int.
First here printf("Hello World") you are missing ;
Second add this part to your main.
int main()
{
printf("Hello World");
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
and when you are calling your function in main,you shouldn't send int a to function b = factorial(int a) ,because by saying int a instead of a you are redefining it.(so it will be uninitialized,if redefinition is not error)
also as said in comments you should add a prototype for factorial before main or move it before main.
Finally your loop in factorial is infinitive ,for(i=1; i < x; i++) since you're doing x *= i;
this condition i < x is never true.
you will increase x until int type has not enough space for it. so a garbage value will be assigned to it ,and you will exit the loop.
This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 3 years ago.
How can I fix this code? I can't input a value for n more than 100000 but I have declared it as long long int.
I tried to solve it but couldn't. Please tell me what is wrong.
#include<stdio.h>
void main() {
long int n;
scanf("%ld",&n);
unsigned long long int a[n];
unsigned long long int max[n];
for(unsigned long long int i=0;i<n;i++) {
scanf("%lld",&a[i]);
}
for(unsigned long long int i=0;i<n;i++) {
unsigned long long int count=0;
for(unsigned long long int j=0;j<n;j++) {
if(a[i]==a[j]) {
count++;
}
}
max[i]=count;
}
for(unsigned long long int i=1;i<n;i++) {
if(max[0]<max[i]) {
max[0]=max[i];
a[0]=a[i];
}
else if(max[0]==max[i]) {
if(a[0]>a[i]) {
a[0]=a[i];
}
}
}
printf("%lld",a[0]);
}
You're declaring variables on the stack that are too big:
unsigned long long int a[n];
unsigned long long int max[n];
These variables are declared local to the function main, meaning that on most implementations they live on the stack. The stack is typically not that big, so when you specify a large value for n it creates arrays on the stack that are too big, overflowing the stack and causing a segfault.
Rather than creating the arrays on the stack, use malloc to create them on the heap:
unsigned long long int *a = malloc(n * sizeof(*a));
unsigned long long int *max = malloc(n * sizeof(*a));
Also, make sure you check the return value of malloc and call free when you're done.
I was practising dynamic programming on SPOJ. I am stuck on this problem named AGS. Below is my code. It is giving runtime error on SPOJ but is running perfectly fine on my computer. Please help
#include<stdio.h>
long long mypow(long long base,long long exp,long long mod,long long log[])
{
if(log[exp]!=0)
return log[exp];
if(exp==0)
return 1;
if(exp==1)
return base%mod;
long long ans = ((base%mod) * mypow(base,exp-1,mod,log))%mod;
log[exp]=ans;
return ans;
}
long long gpsum(long long r,long long k,long long mod,long long log[])
{
if(k==0)
return 1;
if(k==1)
return 1+r;
long long ans=(mypow(r,k,mod,log)+gpsum(r,k-1,mod,log))%mod;
return ans;
}
int main()
{
long long i,t,k,a,d,r,n,mod,p,q,ans;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld %lld",&a,&d,&r);
scanf("%lld %lld",&n,&mod);
long long log[n/2];
for(i=0;i<n/2;i++)
log[i]=0;
if(n==1)
{
ans=a;
}
else if(n%2==0)
{
k=(n-2)/2;
p=mypow(r,k,mod,log);
q=gpsum(r,k,mod,log);
ans=(((p%mod)*(a%mod))%mod+((q%mod)*(d%mod))%mod)%mod;
}
else
{
k=(n-3)/2;
p=r*mypow(r,k,mod,log);
q=r*gpsum(r,k,mod,log);
ans=(((p%mod)*(a%mod))%mod+((q%mod)*(d%mod))%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
It looks like the line long long log[n/2]; causing the error. For n = 12345678 (from the sample input in the comments) it will try to allocate around 6M of long long which is about 50Mb on the stack. Which is, I am guessing, too big for the online multi user compilers. The following code will prove this claim:
#include<stdio.h>
#define MAX 7000000
int main()
{
volatile long long v[MAX];
long i;
for ( i=0; i<MAX; i++ )
v[i]=i+1;
printf("Hello");
return 0;
}
The value of MAX as stated will give the error on ideone. Reducing the value, say to 700000 will remove the error (don't mind the volatile and the loop exercises, they are there to trick on optimizer).
A possible solution would be allocating this memory statically, as a global variable - outside the main function, such that it will go to the .data section, and not to the stack. This solution:
#include<stdio.h>
#define MAX 7000000
volatile long long v[MAX];
int main()
{
long i;
for ( i=0; i<MAX; i++ )
v[i]=i+1;
printf("Hello");
return 0;
}
works just fine even with larger numbers.
This question already has answers here:
Big array gives segmentation error in C
(4 answers)
Closed 9 years ago.
This program is to find the prime number which is is the sum of most number of consecutive prime numbers. When I set the value of LIMIT to 50 100 or even 1000, the correct values are obtained. But if I take the value of LIMIT greater than 0.3 million the program crashes. What is the reason behind this problem and how can I solve it?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LIMIT 300000
//binary search function
int Bsearch(long long int *arr,long long int low,long long int high,long long int search)
{
if(high<low)
return 0;
long long int mid = (low+high)/2;
if(search<arr[mid])
{return(Bsearch(arr,low,mid-1,search));}
else if(search>arr[mid])
{return(Bsearch(arr,mid+1,high,search));}
else if(search==arr[mid]) return 1;
else return 0;
}
int main()
{
long int arr[LIMIT];
arr[0]=0;arr[1]=1;
for(long long int i=2;i<=LIMIT;i++)
{arr[i]=1;}
long long index=2;
for(long long int i=3;i<=LIMIT;i++,index++)
{
for(long long int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{arr[index]=0;}
}
}
long long int count=0;
//calculate total primes
for(long int A=0;A<LIMIT;A++)
{
if(arr[A]==1)
count++;
}
//all primes stored in seperate primes array
long long int primes[count];
for(long int A=0,index=0;A<LIMIT;A++)
{
if(arr[A]==1)
{
primes[index++]=A+1;
}
}
long long int primes_sum[count];
primes_sum[0]=primes[0];
for(long long int B=1;B<count;B++)
{primes_sum[B]=primes_sum[B-1]+primes[B];}
for(long long int i =0;i<(sizeof(primes)/sizeof(long long int));i++)
{
printf("\nprime number : %lld\tprimes_sum : %lld",primes[i],primes_sum[i]);
}
struct ultimata
{
long long int num_of_primes;
long long int prime_number;
}final;
final.num_of_primes=0;final.prime_number=0;
for(long long int j=(sizeof(primes_sum)/sizeof(long long int))-1;j>=2;j--)
{
for(long long int i=j-2;i>=1;i--)
{
long long int search_term = primes_sum[j]-primes_sum[i];
printf("\nsearch term : %lld (primes_sum[%lld]-primes_sum[%lld])",search_term,j,i);
if(search_term>LIMIT)
break;
if(Bsearch(primes,0,(sizeof(primes)/sizeof(long long int)),search_term))
{
printf("\nfound! : %lld terms : %lld",search_term,j-i-1);
if((j-i-1)>final.num_of_primes)
{
printf("\nfound! : %lld",search_term);
final.prime_number=search_term;
final.num_of_primes=j-i-1;
}
}
}
}
printf("Largest prime number : %lld",final.prime_number,final.num_of_primes);
return 0;
}
I think one of your problems is in these lines (multiple times in other places, too):
long int arr[LIMIT];
...
for(long long int i=2;i<=LIMIT;i++)
{arr[i]=1;}
Your array has LIMIT entries, but you try to write to entry LIMIT+1. C starts with array index 0 and ends with size-1.
I don't know why it works for smaller values of LIMIT.
I cannot get the following code to work.
#include <stdio.h>
// I am not sure whethere I should void here or not.
int main() {
// when the first bug is solved, I put here arg[0]. It should be
// similar command line parameter as args[0] in Java.
int a=3;
int b;
b = factorial(a);
// bug seems to be here, since the %1i seems to work only in fprintf
printf("%1i", b);
return 0;
}
int factorial(int x) {
int i;
for(i=1; i<x; i++)
x *= i;
return x;
}
How can you get the code to work?
You're modifying your loop terminating variable (x) inside the loop. Currently your code blows up after a few iterations, when x overflows the range of a 32 bit integer and then becomes negative and very large, hence terminating the loop.
It should be:
int factorial(int n) {
int i, x = 1;
for (i = 2; i <= n; ++i) {
x *= i;
}
return x;
}
Better yet, you should use long instead of int for the variable x and the return value, because n! gets very large very quickly.
AInitak gave the right answer, but I want to add that one way you can find the bug in your code is to print out the values of i and x in the factorial loop.
int factorial(int x) {
int i;
for(i=1; i<x; i++)
{
x *= i;
printf("%d, %d\n", i, x);
}
return x;
}
This gives you the output
1, 3
2, 6
3, 18
4, 72
5, 360
6, 2160
7, 15120
8, 120960
9, 1088640
10, 10886400
11, 119750400
12, 1437004800
13, 1501193216
14, -458131456
-458131456
This makes it easier to see what's going wrong. The loop doesn't stop where you expect it to for the reasons AInitak explained.
It's bad style in C to leave out void when defining or declaring a function. So put it in
int main(void)
While it doesn't change anything about the number of parameters the function has (the function has zero parameters without that void either), it will declare the function as one that accepts only zero arguments, while it won't tell anything about the amount and types of accepted arguments when you omit the void. However, both versions with and without void are correct.
Read this answer about that matter too.
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int x,y;
int *num;
if(c==1)
{
printf("Usage : programName : number");
return 0;
}
num=(int *)malloc(sizeof(int));
*num=atoi(v[1]);
x=1;y=1;
while(x<=*num)
{
y=y*x;
x++;
}
printf("Factorial of %d is %d ",*num,y);
free(num);
return 0;
}
What error message do you get?
First off, declare your function factorial before main. Also, pay attention to correct indentation. Your declaration of function main is correct, by the way.
I would suggest to use also double or unsigned long for factorial computation in order to be able to compute the greater value of the factorial function.
double fact( double n)
{
if ( n == 1)
return 1;
return n*(fact(n-1));
}
A more elegant non-recursive function.
#include<stdio.h>
long long int fact(long long int);
long long int fact(long long int n){
long long int num = 1;
long long int fi = 0;
for(long long int i=2;i<=n;i++){
for(long long int j=1;j<=i;j++){
fi += num;
}
num = fi;
fi = 0;
}
return num;
}
int main(){
long long int n;
scanf("%lld",&n);
printf("%lld\n",fact(n));
return 0;
}