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
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 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 11 months ago.
Improve this question
I want to count numbers in int from user input from char to zero. This code make only 1 cycle. (Please help me understand what is wrong in second part.)
void count_down_from(int num);
int main()
{
int start;
char letter;
printf("print letter to reduce to zero in ACSCII");
scanf("%c", &letter);
count_down_from(letter);
return 0;
}
This part working not good:
void count_down_from(int num)
{
if (num > 0)
{
--num;
count_down_from;
printf("%d\n", num);
}
else
return;
}
}
What behaviour expected:
void count_down_from(int num)
{
printf("%d\n", num);
--num;
if (num < 0)
return;
else
count_down_from(num);
}
take a look at my modifications
void count_down_from(int num)
{
if (num >= 0) //need the >= operator to go down to zero not just 1
{
printf("%d\n", num);
--num;
count_down_from(num); //you need to send num to the call otherwise it won't be a recursive function
}
else
return;
}
Missed (num) in function call in second block.
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 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <array.h>
int col, str;
int *point;
void setArr()
{
printf("Input columns="); scanf("%d", &col);
printf("Input strings="); scanf("%d", &str);
int num[str][col];
for(int i = 0; i < str; ++i)
{
for(int j = 0; j < col; ++j)
{
scanf("%d", &num[i][j];
}
}
point = num;
}
int main(void)
{
setArr();
printf("First=%d\n", *point);
printf("Number=%d", *point);
}
Output:
Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104
Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.
With
point = num;
you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.
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