Thread: Complex numbers and C structures

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    72

    Complex numbers and C structures

    hello, i just began studying C structures and i got an exercice at the first days so i'm still not familiar with it. can anyone plz help.

    The problem is as following:. Represent complex numbers )
    • Implement all 4 functions (absolute value, addition, multiplication, negation)
    • Do this in two different ways, using:
    - Arrays(how can i do this?)
    - Structures

    I hope someone can help me with that

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about you suggest something, then we can comment on it. This will teach you a lot more than one of us giving you an immediate answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Normally classes have some kind of instruction leading up to an assignment, or they have a text-book that you can read to learn how to use the material.

    I would suggest that you start by looking at your notes or reading the book. That's a great place to get started. Then once you have your program written, this forum exists to help you debug it. However, we cannot help you until you have tried to solve the problem on your own.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    72
    well i tried to do the addition fuction with typedef
    Code:
    #include<stdio.h>  
      
       
     struct Complex {  
         float real;  
         float img;  
     };  
       
      
     Complex add_complex( Complex z1, Complex z2 ) {  
       Complex z3;  
       z3.real = z1.real + z2.real;  
       z3.img  = z1.img  + z2.img;  
       return z3;  
     }  
       
     int main() {  
       Complex z1, z2, z3;  
       
        
       printf( "Enter the first complex number \n");  
       printf( "Enter the real part " );  
       scanf( "&#37;f", &z1.real );  
       printf( "Enter the imaginary part " );  
       scanf( "%f", &z1.img );  
       
        
       printf( "Enter the second complex number \n");  
       printf( "Enter the real part " );  
       scanf( "%f", &z2.real);  
       printf( "Enter the imaginary part " );  
       scanf( "%f", &z2.img );  
       
       z3 = add_complex( z1, z2 );  
       //show the sum  
       printf( "sum: %f + i * %f", z3.real, z3.img );  
       
       getchar();    
       return 0;     
     }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you need a typedef, if that's what you mean. struct Complex just defines a new type called "struct Complex" -- if you want Complex, that's where the the typedef comes in. But I don't see anything (else) wrong here.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And how is that working out.

    Technically, by the way, that code shouldn't compile in C since it uses the C++ "skip struct when you use it" mechanism. You can achieve the same thing in valid C by writing
    Code:
    typedef struct tagComplex {  
         float real;  
         float img;  
     } Complex;
    [Technically, you don't have to add "tag" (or anything else) to the struct name, but it makes it more clear what is happening.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    72
    yes it runs normally, however i don't know how to do it with arrays if this questions can be answered plz help

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    My best guess would be that your instructor wants you to use

    float complex[2];

    for an array implementation of complex numbers.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A complex number is two numbers, right. How would you store two numbers as an array?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    72
    i see that make thing visual for me thanks i'll get to work now thanks

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    72
    hello guys i got this exercice in which i need help. This program should fill two arrays made of information about students and employee. i have to sort alphabetically each of them by name and last name, still i have to check that no name should be repeated. In this program i tried to store the information in a table string"std" and in the same time check for any repeated name but i have problem with the sorting and when i run this it shuts down as the size ins't recognized even if i specified it.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define SIZE 10; 
    typedef struct {
             char *firstname;
             char *lastname;
             float grade;
             }student;
             
    typedef struct {
            char *lastname;
            char *firstname;
            float salary;
            }employee;
    void fill_1(student []);        
    void fill_s(student);
    void fill_2(employee *);
    void fill_e(employee []);
    int search(student [],student );
    int main()
    {
        student s[10];
        student std;
        fill_1(s);
        
        }
    
    
    
    
    
    
    
    
    
    
    void fill_e(employee *e)
    {
         printf("please enter first name");
         scanf("&#37;s",&e->firstname);
         printf("please enter last name");
         scanf("%s",&e->lastname);
         printf("please enter grade");
         scanf("%f",&e->salary);
         
         } 
    
    
    
    
    void fill_s(student *s)
    {
         printf("please enter first name");
         scanf("%s",&s->firstname);
         printf("please enter last name");
         scanf("%s",&s->lastname);
         printf("please enter grade");
         scanf("%f",&s->grade);
         
         }        
    
    
    void fill_1(student s[])
    {
         int i=0,test;
         student std;
         fill_s(&std);
         s[0]=std;
         
        
          do{
              fill_s(&std);
              if(search(s,std)==1) printf("no");  
               else s[i+1]=std;
                      
               i++;
               printf("entrez 0,1");
               scanf("%d", &test);   
                 
                 
                }while((i<10)&&(test==1));
                         
    }
    
     
     
     void fill_2(employee e[])
    {
       int i=0,test; 
        do 
           { 
              fill_e(&e[i]);
              printf("if you want to add another employee press 1 else press 0");
              scanf ("%d",&test);                 
              i=i+1;
              }while((i<10)&&(test==1));                      
     
    }
     int search(student s[],student std)
     {
         
         for(int i=0;i<10;i++)
         {
             if ((strcmp(s[i].firstname,std.firstname)==0) && (strcmp(s[i].lastname,std.lastname)==0))    
                 return 1;
                 
         
         
         } return 0;}

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    72
    i get many errors for sure

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    #define SIZE 10;
    without the ;. Or else it will put everywhere it sees SIZE, 10; which will give you errors ofc.

    EDIT:
    You use scanf() wrongly in this code:
    Code:
    void fill_s(student *s)
    {
         printf("please enter first name");
         scanf("&#37;s",&s->firstname);
         printf("please enter last name");
         scanf("%s",&s->lastname);
         printf("please enter grade");
         scanf("%f",&s->grade);
         
         }
    You dont need the &. Jst s->something. You declare s as a pointer to a student structure. That is why you use correctly the -> symbol. The & is wrong. It is used to give the address of a variable (rarely used with a pointer).

    Post the errors if you want, I don't see anything else with a quick look, so if you still have errors say so to look the code more thoroughly. And run-time erros or it doesn't compile?
    Last edited by C_ntua; 09-11-2008 at 01:55 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Really? I get none (compile errors).
    But there are a couple of things you really should look into/change and add.
    First of, scanf to read strings is unsafe. Do it properly or not at all. See my signature.
    Then read: http://cpwiki.sourceforge.net/Common...llocating_them
    And then I will give a suggestion. Don't strip the names of the arguments from the declarations. Don't. Declarations and the definitions should look alike.
    And don't use conio.h. It's an evil non-standard header.

    And of course you must NOT use & on s->firstname and s->lastname, because that would create a pointer-to-pointer (char**) and scanf wants a pointer (char*), simply put.

    And std defined in main is used nowhere.
    Last edited by Elysia; 09-11-2008 at 02:12 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You dont need the &. Jst s->something.
    it still needed for the grade
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed