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
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 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.
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 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 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);
}
}
}
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 6 years ago.
Improve this question
loops for structures
main()
{
structure perdata //MADE A STRUCTURE
{
char name;
int age;
float salary;
}p1,p2,p3;
int i;
for(i=1;i<4;i++)
{
printf("p%d.name",i);
scanf("%s",);/*should loop and read names of p1,p2,p3*/
}
printf("p1.name:%s",p1.name);
getch();
}
You should use array of structures so that you may iterate through it.
Example : An array of student structure,
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10]; //Ten student details maybe stored
int i;
printf("Enter information of students:\n");
//get the details of 10 students
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
//display the details of 10 students
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}
Use the above sample code & write your program.
Always make arrays of structures, classes etc. whenever you need to loop through instances.
int main(void)
{
struct perdata //MADE A STRUCTURE
{
char name[20];
int age;
float salary;
};
struct perdata p[4];
for(int i=0;i<4;i++)
{
//printf("p%d.name",i);
scanf("%s", &p[i].name);/*should loop and read names of p1,p2,p3*/
}
for(int i=0;i<4;i++)
{
printf("p%d.name:%s\n",i+1, p[i].name);
}
getch();
}