How to loop for Structures? [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 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();
}

Related

How do I return an array of integers in C? [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 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?

how to print all id names in one loop?

This code is about 'struct' in C..
I created a struct with the properties name,roll etc.. By using the for-loop I let the user to create the struct objects. they are named as student,employee, faculty
the problem is the objects are created. But I can use them only inside the for-loop. If I want to get the value of all names in main function, it doesn't work. How can I solve it?How do i print all names in the code in only one loop
what will be the logic
#include<stdio.h>
#include<conio.h>
struct student
{
int std;
char fee[90]; //Collect Data of students
int rollno;
char name[15];
char sub[100];
};
main()
{
int x;
printf("******Enter the total number of Student from HOD*******:\n");
scanf("%d",&x);
struct student a[x];
for(int i=0;i<x;i++)
{
printf("\nEnter Rollno:\t");
scanf("%d",&a[x].rollno);
printf("\nEnter name:\t");
scanf("%s",&a[x].name);
printf("\nIs Fee Submitted:\t");
scanf("%s",&a[x].fee);
printf("\nEnter Subject name:\t");
scanf("%s",a[x].sub);
}
printf("\n****Display All Student names****");
for(int i=0;i<x;i++)
{
printf("\n%s",a[x].name);
}
//Faculty
struct faculty
{
char Fname[100];
char Sname[100];
};
int y;
printf("\n\n********Please HOD enter the total faculty members********\n");
scanf("%d",&y);
struct faculty b[y];
for(int j=0;j<y;j++)
{
printf("\nEnter Faculty Member Name:\t");
scanf("%s",&b[y].Fname);
printf("\nEnter their Subjects:\t");
scanf("%s",&b[y].Sname);
}
printf("\n****Display all Faculty Member Name****");
for(int j=0;j<y;j++)
{
printf("\n%s",b[y].Fname);
}
// Employes
struct employes
{
char ename[100];
char rank[100];
};
int z;
printf("\n\n********please HOD enter the total no of Employes*******:\n");
scanf("%s",&z);
struct employes c[z];
for(int j=0;j<y;j++)
{
printf("\nEnter the Employe name:\t");
scanf("%s",&c[y].ename);
printf("\n and enter their ranks:\t");
scanf("%s",&c[y].rank);
}
printf("\n****Display all Employe names****");
for(int j=0;j<y;j++)
{
printf("%s\n",c[y].ename);
}
}
#include<stdio.h>
#include<conio.h>
struct student
{
int std;
char fee[90];
int rollno;
char name[15];
char sub[100];
};
main()
{
int x;
printf("******Enter the total number of Student from HOD*******:\n");
scanf("%d",&x);
struct student a[x];
for(int i=0;i<x;i++)
{
printf("\nEnter Rollno:\t");
scanf("%d",&a[x].rollno);
printf("\nEnter name:\t");
scanf("%s",&a[x].name);
printf("\nIs Fee Submitted:\t");
scanf("%s",&a[x].fee);
printf("\nEnter Subject name:\t");
scanf("%s",a[x].sub);
}
printf("\n****Display All Student names****");
for(int i=0;i<x;i++)
{
printf("\n%s",a[x].name);
}
struct faculty
{
char Fname[100];
char Sname[100];
};
int y;
printf("\n\n********Please HOD enter the total faculty members********:\n");
scanf("%d",&y);
struct faculty b[y];
for(int j=0;j<y;j++)
{
printf("\nEnter Faculty Member Name:\t");
scanf("%s",&b[y].Fname);
printf("\nEnter their Subjects:\t");
scanf("%s",&b[y].Sname);
}
printf("\n****Display all Faculty Member Name****");
for(int j=0;j<y;j++)
{
printf("\n%s",b[y].Fname);
}
struct employes
{
char ename[100];
char rank[100];
};
int z;
printf("\n\n********Please HOD enter the total no of Employes*******:\n");
scanf("%d",&z); //You used %s instead of %d
struct employes c[z];
for(int j=0;j<z;j++) //You used wrong variable 'y' here
{
printf("\nEnter the Employe name:\t");
scanf("%s",&c[z].ename); //You used wrong variable 'y' here
printf("\nEnter their ranks:\t");
scanf("%s",&c[z].rank); //You used wrong variable 'y' here
}
printf("\n****Display all Employe names****");
for(int j=0;j<z;j++) //You used wrong variable 'y' here
{
printf("\n%s",c[z].ename); //You used wrong variable 'y' here
}
}
Errors:
Wrong format specifier used in taking integer input in number of Employees.
Wrong loop variable used in all loops of Employee.
Now this code is working. I've pointed out the errors in the comments also.
The loop increases i from 0 to some other value x.
(lets say x is 5 for this example)
So i has values 0, 1, 2, 3....
But regardless, you only put data into one element of the array: 5
And you keep overwriting that same element over and over again.
And 5 isn't even a valid index!! The valid indicies are 0, 1, 2, 3, 4.
5 is one-past the end of the array!

with in C, access variable in structure [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
I have the following code, which is not how to implement in C, (if it has one, the keyword with.On the other hand because the program does not access the data type structure, it should not give an error because the variables partial_n2, final_n2, name2 are defined in the struct.
The program has to store in an array of records the names of the students, their partial and final grades. Find the average grade and show a message of SUIT if the student exceeds or equals the grade of 5 or NOT SUIT if it is not enough. Do it for a number of 5 students.
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//PROGRAM EJER009
#define numstudents 5
typedef struct notas{
char name2[20];
float partial_n2, final_n2;
}tnotas;
tnotas notas[numstudents];
tnotas clase;
char name[20];
float partial_n, final_n, n_media;
int i;
int main(){
for (i = 0; i <= numstudents;i++)
{
printf("Enter the student's name% d: ",i);
scanf("%s",name);
printf("Enter your partial note: ");
scanf("%f",&partial_n);
printf("Enter your final note: ");
scanf("%f",&final_n);
printf("\n");
with (clase[i])
{
partial_n2 = partial_n;
final_n2 = final_n;
name2 = name;
}
}
printf("cls");
printf("NAME\tPartial\tFinal\tMedia\tQUALIFICATION\n");
for (i = 1; i<=numstudents;i++){
with clase[i]
{
n_media = (partial_n2 + final_n2) / 2;
printf("%d %d %d",name2,partial_n2,final_n2);
system("color 14"); printf("%lf",n_media);
if (n_media >= 5)
{
system("color 11");
printf("SUITABLE :-)");
}
else
{
system("color 1");
printf("NOT SUITABLE :-(");
}
system("color 7");
}
}
getch();
return 0;
}
You can read a value of a member by:
float f;
f = notas[0].partial_n2;
You can write a value of a member by:
notas[0].partial_n2 = 10.3;

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

Define a function to get the elements of 2D matrix from user in C [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 8 years ago.
Improve this question
I can't finish this code please help me! I have to matrix, and the program reads items of matrix in function
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int m1[r1][c1];
GetArray(m1,r1,c1);
system("PAUSE");
return 0;
}
void GetArray(int arr[][],int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i==0;i<_row;i++){
for(j==0;j< _column;j++){
scanf("%d",&num);
arr[i][j]=num;}} //give error in this line
}
In C programming, == is used for comparison and = is used for assignment operations. You would definitely want to assign values to j and i in your for loops. In your case, you are not initializing the loop variables (when you declare them in the beginning of the function) and since they get garbage values when they are not initialized, you try to reach beyond the bounds of the array you are using in the for loops, thus getting a segmentation fault.
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int **m1;
for(int i = 0; i<r1 ;++i) // use c99 !
m1[i] = malloc(c1* sizeof(int));
GetArray(m1,r1,c1);
system("PAUSE");
for(int i = 0; i<r1 ;++i)
free(m1[i]);
return 0;
}
void GetArray(int ** m1,int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i=0;i<_row;i++){
for(j=0;j< _column;j++){
scanf("%d",&num);
m1[i][j]=num;}} //give error in this line
}
Untested

Resources