Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm getting an error in this code - invalid conversion from int* to int how can i correct this?
#include <stdio.h>
#include <conio.h>
void walk(int,int,int,int,int,int,int);
int main()
{
int n,i;
scanf("%d",&n);
int a[10],b[10],c[30];
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]); b[i]=0;
}
int j=1;
walk(1,j,0,b,c,n,a);
for(i=1;i<=j;i++)
{
if(c[i]==0)
printf("%d");
else printf("%d",c[i]);
}
getch();
}
void walk(int i,int j,int s,int b[],int c[],int n,int a[])
{
c[j]=a[i];
j++;
if(b[a[i]]!=1)
{
b[a[i]]=1;
walk(a[i],j,s,b,c,n,a);
} else {
c[j]=0;
s++;
i=1;
while(b[i]==1&&i<=n)
i++;
if(i<=n)
walk(i,j,s,b,c,n,a);
else printf("%d",s);
}
}
You're declaring walk as void walk(int,int,int,int,int,int,int) but in the definition you're using some int[] arguments.
Related
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 3 days ago.
Improve this question
#include<stdio.h>
#include<stdlib.h>
int *CreatePoly(int order);
int *AddPoly(int P[],int Q[],int orderP, int orderQ);
void Print(int A[], int order);
int main()
{
int orderP, orderQ;
printf("\nEnter order of first Polynomial");
scanf("%i",&orderP);
printf("\nEnter order of second Polynomial");
scanf("%i",&orderQ);
int P[]=CreatePoly(orderP);
Print(P,orderP);
int Q[]=CreatePoly(orderQ);
Print(Q,orderQ);
int R[]=AddPoly(P,Q,orderP,orderQ);
Print(R,(orderP>orderQ?orderP:orderQ));
}
int *CreatePoly(int order)
{ int *P=malloc(2*order);
for(int i=0;i<2*order;i+=2)
{
printf("\n Enter Power");
scanf("%i",&P[i]);
printf("\nEnter Coefficient");
scanf("%i",&P[i+1]);
}
return P;
}
int *AddPoly(int P[], int Q[], int ormerP, int orderQ)
{
int i,j,k;
int orderR=(orderP>orderQ?orderP:orderQ);
int R[2*orderR];
while(i<2*orderP&&j<2*orderQ&&k<2*orderR){
if (P[i]>Q[j]) {
R[k]=P[i];
R[k+1]=P[k+1];
i+=2;
k+=2;
}
else if (P[i]<Q[i]) {
R[k]=Q[j];
R[k+1]=Q[j+1];
j+=2;
k+=2;
}
else{
R[k]=P[i];
R[k+1]=P[i+1]+Q[i+1];
i+=2;
j+=2;
k+=2;
}
}
return R;
}
void Print(int P[], int order)
{
for (int i = 0; i < 2*order; i+=2) {
printf("%dx^%d",P[i+1],P[i]);
}
}
Poly1.c:14:6: error: array initializer must be an initializer list or wide
string literal int P[]=CreatePoly(orderP);
^ Poly1.c:16:6: error: array initializer must be an initializer list or wide
string literal int Q[]=CreatePoly(orderQ);
^ Poly1.c:60:10: warning: address of stack memory associated with local
variable 'R' returned [-Wreturn-stack-address] return R;
^
I'm supposed to use only arrays in the program how do i solve this?
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 months ago.
Improve this question
sopfr
I tried making a code that calculates the mean from sopfr(c) up to sofr(c+i), but when I input the numbers nothing happens
This is the code ive done
#include <stdio.h>
int sopfr(int x);
int main()
{
int c ,i ,sum1=0, a=0;
scanf("%i",&c);
scanf("i",&i);
for(c;c<=c+i;a++)
{
sum1+=sopfr(c);
c+=a;
}
float sum2=sum1/i;
printf("%f",sum2);
return 727;
}
int sopfr(x)
{
int t, n=2, s=0;
scanf("%i",&t);
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}
Remove the scanf() call and make t the function parameters.
int sopfr(int t)
{
int n=2, s=0;
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}
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 5 years ago.
Improve this question
Can anyone tell what I am doing wrong here?
Problem statement:
https://practice.geeksforgeeks.org/problems/good-or-bad-string/0
My code:
#include <stdio.h>
#include<string.h>
int is_vowel(char a) {
if(a==97||a==101||a==105||a==111||a==117){
return(1);
}
return(0);
}
int main() {
//code
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++){
char str[100];
scanf("%s",str);
printf("%s",str);
int c_cnsnt=0;
int c_vwl=0;
int g_b=1;//suppose good
for(int j=0;j<strlen(str);j++){
//("%c",str[j]);
int num=is_vowel(str[j]);
printf("Debug %c %d %d\n",str[j],num,strlen(str));
if(is_vowel(str[j])) {
c_vwl++;
}
else { c_cnsnt++;}
if(c_vwl==c_cnsnt){
c_cnsnt=0;
c_vwl=0;
}
else {
if(c_vwl>5||c_cnsnt>=3){
g_b=0;
break;
}
}
}
printf("%d\n",g_b);
}
return 0;
}
Sample
Input:
2
aeioup??
bcdaeiou??
Output:
1
0
My solution link:
https://code.hackerearth.com/9bca55K
Why does the for loop not work for the 2nd string?
Hint: You have to clear the the consonant and vowel counts after increment the other (e.g {c_vwl++;c_cnsnt=0;}), not when they are equal, and always tests your BAD condition.
I will not give you a sample code. Good luck
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was unable to solve this problem, tried much but logic not working.
Problem is,
I have to calculate all possible combinations of 2 x N matrix.
conditions:
sum of all elements must be N.
elements in a row or a column must be in non increasing way.
all elements must be positive and real numbers.
print all possible combinations.
let for 6, it is 29
thanks.
This problem is already asked,
you can use backtracking to solve it
here's the code,
#include<conio.h>
#include<stdio.h>
int a[2][100],c,sum,num;
int ch;
int check(int x,int y)
{
int i=1;
if(x==1&&a[x][y]>a[0][y])
i=0;
if(y>0&&a[x][y]>a[x][y-1])
i=0;
return i;
}
void print()
{
int i,j;
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<num;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
void fun(int lim,int x,int y)
{
int i;
if(y<num)
for(i=lim;i>0;i--)
{
a[x][y]=i;
if(check(x,y))
{
sum+=a[x][y];
if(sum==num)
{
print();
sum-=a[x][y];
a[x][y]=0;
c++;
}
else
{
fun(num-sum,(x+1)%2,y+(x+1)/2);
a[(x+1)%2][y+(x+1)/2]=0;
fun(num-sum,(x+2)%2,y+(x+2)/2);
a[(x+2)%2][y+(x+2)/2]=0;
}
sum-=a[x][y];
}
}
}
int main()
{
scanf("%d",&num);//num=6
fun(num,0,0);
printf("%d",c);
return 0;
}
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
Where is it logically wrong? I don't find it incorrect but the output it
gives is just 1. It should give all the Armstrong number from 1 to 500.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=0 ,d,i=1;
while(i<=500)
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
if(c==i)
` printf("%d",i);
i++;
}
getch();
}
You need to initialize c before the inner loop:
while(i<=500)
{
b=i;
c=0; /* reset 'c' */
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
}
You are using non-standard signature for main(). See: What should main() return in C and C++?
if you run the following code
you will see why there is only one output.
Note: correct declaration of main()
Note: using common functions rather than the proprietary conio.h
Note: uses simple 'for' statement rather than 'while' and increment of 'i'
#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>
int main()
{
//clrscr();
int a;
int b;
int c=0;
int i=1;
for( ; i<=500; i++ )
{
b=i;
while(b>0)
{
a=b%10;
c=(a*a*a)+c;
b=b/10;
}
printf( "a=%d. b=%d, c=%d\n", a,b,c);
if(c==i)
printf("%d\n",i);
} // end for
//getch();
getchar();
return(0);
} // end function main