Jump to content

structures in C

- - - - -

  • Please log in to reply
3 replies to this topic

#1
huhz

huhz

    Newbie

  • Members
  • Pip
  • 2 posts
Hello, I had a task to write a code for given task. I wrote it and everything was okey, but now I have to rewrite that same code with the help of structures and I have some problems doing that and I really can't figure out that, because I don't have a big experience in C and materials which I have doesn't help a lot. So, I am going to add code of the first task which worked as planed:

# include <stdio.h>

# include <ctype.h>

# include <conio.h>  


float A, formul1, formul2;


int main() 

{

int Ae = 0;

                         

  printf("Enter exam mark: ");

  scanf("%d", &Ae);   

  getchar();

         while ( Ae < 1 || Ae > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Ae);  

         getchar();

};

printf("Data correct: %d\n", Ae);


int Alab = 0;

                         

  printf("Enter laboratory work mark: ");

  scanf("%d", &Alab);   

  getchar();

         while ( Alab < 1 || Alab > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Alab);  

        getchar();

};

printf("Data correct: %d\n", Alab);

  

int Aref = 0;

                         

  printf("Enter report mark: ");

  scanf("%d", &Aref);   

  getchar();

         while ( Aref < 1 || Aref > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Aref);  

         getchar();

};

printf("Data correct: %d\n", Aref);

  

formul1 =( ( 0.6*Ae ) + ( 0.3*Alab ) + ( 0.1*Aref ) );

formul2 = Ae;

if ( Ae >= 4 ) A = formul1; else A = formul2;

  

  printf("%2.1f - Exam mark\n",A);

  getche();

  return 0;

}

So I started to rewrite this code and did some of the work, but then I got some problems and now i can't finish this work because of those problems. So I am going to add my code(not finished) for second work:

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <windows.h>

#define N 25


int menu();


int main()

{


struct list

   {

      int nr;

      char name[20], surname[20], exam_mrk[2], laboratory_mrk[2], report_mrk[2];

      float mrk;

   } student[N];

int i,j,x;



while (x != 4)

 { 

x = menu();


switch(x){

          case 1: printf("blablabla");             

                  getch();

                  system("cls"); 

                  break; 

               

          case 2: printf("Save information about students which includes: ");

                  printf("Student name, surname, marks, final mark");

                  printf("(according to the first code)");

                  printf ("provide opportunity to list all unsuccessful students ");

                  getch();

                  system("cls");

                  break; 

                  

          case 3: printf("Fill the list please\n");

                  for(i=0; i<N; i++)

                  {

                           student[i].nr = i+1;

                           printf("\nEnter %d. student information\n", i+1);

                           printf("Name: ");

                           gets(student[i].name);

                           printf("Surname: ");

                           gets(student[i].Surname);

                           printf("Exam mark: ");

                           gets(student[i].exam_mrk);

                           printf("Laboratory work mark: ");

                           gets(student[i].laboratory_mrk);

                           printf("Report mark: ");

                           gets(student[i].report_mrk);

                                                          

                                                                   


           system("cls");

           printf("List of students\n");

           for(i=0; i<N; i++)

           printf("%2d%15s%15s%2d%2d%2d%4.2f\n", student[i].nr, student[i].name, student[i].surname, student[i].exam_mrk, student[i].laboratory_mrk, student[i].report_mrk, student[i].mrk);

           

           

           getch();

           return 0;

                     

          case 4: for(i=0; i<N; i++)

                  {

                  if((student[i].mrk)<4)

                      printf("%2d%15s%15s%2d%2d%2d%4.2f\n", student[i].nr, student[i].name, student[i].surname, student[i].exam_mrk, student[i].laboratory_mrk, student[i].report_mrk, student[i].mrk);  

                  }

          getch();

          system("cls"); 

          break; 

          

          case 5:

          return 0;

          break;

          

          default:

          printf("You entered wrong symbol");

          break;

                

                printf("\n Any key to continue !! \n");

                getch();

                system("cls");

  

   }              

  }

 }

}


int menu() 

     {

     int q;                  

     printf("Menu\n");

     printf("Choose operation:\n");

     printf("1.Info about creator\n");

     printf("2.Task terms\n");

     printf("3.Start program\n");

     printf("4.List all unsuccessful students\n");    

     printf("5.Quit\n\n");   

     scanf("%d",&q);

     return q;

     }

So, I have following problems:
1. I can't figure out how to rewrite function from 1. task which calculates final mark in the second work(how to rewrite this - formul1 =( ( 0.6*Ae ) + ( 0.3*Alab ) + ( 0.1*Aref ) );
formul2 = Ae;
if ( Ae >= 4 ) A = formul1; else A = formul2;)
2. And the second problem is, i don't know rewrite code, to set restriction for entered symbols, that is, restrictions for numbers only
I would be very thankful if someone could help me with this, because without of these 2 things I can't finish my work, and the problem is that I can't figure these 2 problems out.

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

It has been a bit difficult to determine what problem you're facing. is that possible that you could highlight the part of the code from your first program that you want to re-write for structured program?

Thanks,

Munir

#3
huhz

huhz

    Newbie

  • Members
  • Pip
  • 2 posts
I need to rewrite this part:
formul1 =( ( 0.6*Ae ) + ( 0.3*Alab ) + ( 0.1*Aref ) );
formul2 = Ae;
if ( Ae >= 4 ) A = formul1; else A = formul2;
Well, I need that formula in the new code for calculating final mark for students and then print it ( if ( Ae >= 4 ) A = formul1; else A = formul2; )
And the second thing is, that i need this restriction code:
   while ( Aref < 1 || Aref > 10 ) 
{     
         printf("Enter a number(border from 1 to 10):");   
         scanf("%d", &Aref);  
         getchar();
Which allows only to enter a numbers

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

based on your input, I've understood that you need to convert the code using structures. You may to do followings

Define a structure


struct Marks

{

  double A;

  double formula1;

  double formula2;

};


so your code would look like when it used the above structure


# include <stdio.h>

# include <ctype.h>

# include <conio.h>  



[COLOR="Red"]Marks ExamMarks;[/COLOR]

int main() 

{

int Ae = 0;

                         

  printf("Enter exam mark: ");

  scanf("%d", &Ae);   

  getchar();

         while ( Ae < 1 || Ae > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Ae);  

         getchar();

};

printf("Data correct: %d\n", Ae);


int Alab = 0;

                         

  printf("Enter laboratory work mark: ");

  scanf("%d", &Alab);   

  getchar();

         while ( Alab < 1 || Alab > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Alab);  

        getchar();

};

printf("Data correct: %d\n", Alab);

  

int Aref = 0;

                         

  printf("Enter report mark: ");

  scanf("%d", &Aref);   

  getchar();

         while ( Aref < 1 || Aref > 10 ) 

{     

         printf("Enter a number(border from 1 to 10):");   

         scanf("%d", &Aref);  

         getchar();

};

printf("Data correct: %d\n", Aref);

  

[COLOR="Red"]ExamMarks.formul1 =( ( 0.6*Ae ) + ( 0.3*Alab ) + ( 0.1*Aref ) );

ExamMarks.formul2 = Ae;

if ( Ae >= 4 ) 

ExamMarks.A = formul1;

 else ExamMarks.A = formul2;

  

[/COLOR][COLOR="Red"]  printf("%2.1f - Exam mark\n", ExamMark.A);

[/COLOR]  getche();

  return 0;

}


I hope that it helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users