2017-04-20 1 views
-1

私はコードを書いたが、削除と検索の方法は知らない。助けてください。私は削除と検索に悩まされています。削除と検索ができるように、コードのいくつかの変更を提案してください。C言語の構造を使って学生情報(名前、ロール番号、マーク)を保存し、挿入、削除、検索を実行するプログラム

#include<stdio.h> 
#define SIZE 50 

struct student { 
    char name[30]; 
    int rollno; 
    int sub[3]; 
}; 

main() { 
    int i, j, max, count, total, n, a[SIZE], ni; 
    struct student st[SIZE]; 


    printf("Enter how many students: "); 
    scanf("%d", &n); 

    /* for loop to read the names and roll numbers*/ 
    for (i = 0; i < n; i++) { 
     printf("\nEnter name and roll number for student %d : ", i); 
     scanf("%s", &st[i].name); 
     scanf("%d", &st[i].rollno); 
    } 

    /* for loop to read ith student's jth subject*/ 
    for (i = 0; i < n; i++) { 
     for (j = 0; j <= 2; j++) { 
      printf("\nEnter marks of student %d for subject %d : ", i, j); 
      scanf("%d", &st[i].sub[j]); 
     } 
    } 

    /* (i) for loop to calculate total marks obtained by each student*/ 

    for (i = 0; i < n; i++) { 
     total = 0; 
     for (j = 0; j < 3; j++) { 
      total = total + st[i].sub[j]; 
     } 
     printf("\nTotal marks obtained by student %s are %dn", st[i].name,total); 
     a[i] = total; 
    } 

    /* (ii) for loop to list out the student's roll numbers who 
    have secured the highest marks in each subject */ 

    /* roll number who secured the highest marks */ 

    for (j = 0; j < 3; j++) { 
     max = 0; 
     for (i = 0; i < n; i++) { 
      if (max < st[i].sub[j]) { 
       max = st[i].sub[j]; 
       ni = i; 
     } 
     } 
     printf("\nStudent %s got maximum marks = %d in Subject : %d",st[ni].name, max, j); 
    } 

    max = 0; 

    for (i = 0; i < n; i++) { 
     if (max < a[i]) { 
      max = a[i]; 
      ni = i; 
     } 
    } 

    printf("\n%s obtained the total highest marks.", st[ni].name); 

} 
+1

に基づいて誰かを削除するには、このスニペットを使用することができます++ 。 1)コードを修正し、2)無関係な言語のタグを削除します。言語C/C++は存在しません。 3)[ask]を読んで助言に従ってください。 – Olaf

+0

何を検索しますか? – lU5er

+0

申し訳ありません私はちょうどC言語を意味しました、申し訳ありませんが明確ではない –

答えて

0

あなたはロール番号

int sroll, flag = 0,  
scanf("%d",&sroll); 
for(i=0;i<n;i++)   
{ 
    if(st[i].rollno == i) // Checking if student exists based on roll 
    { 
     flag = 1; 
     break; 
    } 
} 
if(flag == 0) 
    printf("not found\n"); 
else 
    printf("found\n"); 

に基づいて学生を検索するには、このコードを使用し、ロール番号、有効なCやCもないのです

printf("Enter the roll number of the student whom you want to delete\n"); 
scanf("%d",&droll); 
for(i=0;i<n;i++) 
{ 
    if(st[i].rollno != droll) // Not interested till the roll appears 
     continue; 
    st[i] = st[i+1];    // Shifting one student to left to compensate deletion 
} 
n--;        // Decreasing the total students 
for(i=0;i<n;i++) 
    printf("%s",st[i].name);  // Printing the students present 
関連する問題