Beginner C programming [closed] - c

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
I'm trying to find all the numbers that add up to 10. I'm using a nested for loop. What am I doing wrong?
#include<stdio.h>
int main() {
int i = 0;
int j = 0;
int numOne[10];
int numTwo[10];
for(i=0;i<10;i++){
for(j=0;j<10;j++){
if((numOne[i]+numTwo[j]) == 10){
printf("%d\n",numOne[i]);
printf("%d\n",numTwo[j]);
}
}
}

You don't need arrays
for(i=0;i<=10;i++){
for(j=0;j<=10;j++){
if(i+j == 10){
printf("%d+%d\n",i,j);
}
}
}

Related

How can I draw a circle in TGA, if we know a radius and coordinates x,y? [closed]

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 years ago.
Improve this question
#include <stdio.h>
#include <math.h>
int main ()
{
int i,j,k,r=5,x=20,y=20,d,a,l;
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
a=((i-x)(i-x))+((j-y)(j-y));
d=sqrt(a);
if(r>=d)
{
printf("*");
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
The problem in your code is the line a=((i-x)(i-x))+((j-y)(j-y)); you didn't use the multiplication operator, *. It should be like this a = ((i-x)*(i-x)) + ((j-y)*(j-y));.

The meaning of [i] variable in this sitiuation? [closed]

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 years ago.
Improve this question
Whats the meaning of [i] in the following example?
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]); // HERE
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
i here is a variable.
In your code, [i] acts as the index of values and is used to access the element in array values.
Edit:
Since there is a //HERE comment in your code, im going to assume you would also want what [i] does there. The expression &value[i] basically gives the address of value[i] ,i.e, the "ith" element of the array.

C - Cannot iterate through second string onward [closed]

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

How to get all possible combination of 2xn matrix [closed]

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;
}

fgets is not working properly [closed]

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 9 years ago.
Improve this question
fgets is not working as I expected.
typedef struct {
int itemnumber;
char name [50];
double price;
int stock;
int discount;
int reorder;
int reorderquantity;
} item;
item x;
item *px[n];
px[n] = malloc(sizeof(item));
printf ("ENTER THE NUMBER OF ITEMS\n\n");
scanf ("%d",&n);
for (i=0; i<n; i++)
{
px[i]=&x;
scanf ("%d",&px[i]->itemnumber);
fgets(px[i]->name,50,stdin);
px[i]->name[strlen(px[i]->name)-1]='\0';
// fflush(stdin);
printf("%s",px[i]->name);
}
You only malloc one instance.
you should do px[n] = malloc(sizeof(item)*n); after you scanf n

Resources