need help

Discussion in 'Software' started by skyler43@123, Mar 8, 2012.

  1. skyler43@123

    skyler43@123 Private E-2

    hi everyone.. i have a problem..
    i have 2 lists, with complex numbers, and i have to make another list, with the sum of them...first number form list1 with the first numver form 2-nd one. and so on..
    i don't like pointers, so my program doesn't work:(

    function for sum, void type:
    ...
    Code:
    s=new nod;
    s->info.re=q1->info.re+q2->info.re; 
    s->info.im=q1->info.im+q2->info.im;
    q1=p1; // p1 and p2 are for the begin of the list, and u1, u2-for the end
    q2=p2;
    q3=s;
    while(q1!=u1 && q2!=u2)
    {
    q3=new nod;
    q3=q3->next;
    q1=q1->next;
    q2=q2->next;
    q3->info.re=0;
    q3->info.im=0;
    q3->info.re=q1->info.re+q2->info.re;
    q3->info.im=q1->info.im+q2->info.im;
    }
    u3=q3;
    
    please some help..
     
  2. _nullptr

    _nullptr Major Geeky Geek Geek

    I gather this is C programming?

    *give your variables a meaningful name, at present it's very difficult to tell
    exactly what is what in your code.

    *define the structs that you are using for your lists.

    Code:
    while (q1!=u1 && q2!=u2)   // where are the values of u1 and u2 set?
    {
        q3 = new nod;
        q3 = q3->next;
        q1 = q1->next;
        q2 = q2->next;
        // q3->info.re = 0; // no need to initialize
        // q3->info.im = 0; // these values are set in the next two lines
        q3->info.re = q1->info.re + q2->info.re;
        q3->info.im = q1->info.im + q2->info.im;
    }
    u3=q3;
    
    If you can make your code more understandable, I'll be more than happy to help :)
     
  3. skyler43@123

    skyler43@123 Private E-2

    here is all:
    i have a two dimensional array with complex numbers, memored in a list, and i have to make "+", "-", "*" between two of them.
    obiviously, i can make "+", and so on, only if they have the same numbers of lines and columns.

    for the first list i have p1 for the begining, u1 for the end, and q1 for crossing the list.
    again p2, q2, u2, for the second one, and for sum i tried sth like: s-for the begin, u3-end, and q3 for crossing.
    the program work fine, only the sum is....a big big sh**.
    so i have the lists created, and i have to create the list of sum, and i don't understand how it should be set on that pointers..
    somebody helped me with sth, but after that the program assembly only the re. part of the numbers, and only the im. part, which is totally wrong, because it must assembly every two numbers and make a 3-list, not all in a huge sum.


    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    
    struct complex
    {
    
    double re, im;
    };
    struct nod
    {
    complex info;
    nod *next;
    };
    nod *p1, *p2, *u1, *u2, *u3, *s;
    int n1, m1, n2, m2;
    //---------------------------------------------------------------------------
    void create_list1()
    {
    nod *q1;
    if(!p1)  
    {
    
    p1=new nod;
    cout<<"re= ";
    cin>>p1->info.re;
    cout<<"im= ";
    cin>>p1->info.im;
    u1=p1; 
    }
    else 
    {
    
    q1=new nod; 
    cout<<"re= ";
    cin>>q1->info.re;
    cout<<"im= ";
    cin>>q1->info.im;
    u1->next=q1;
    u1=q1;
    }
    u1->next=0;
    }
    //----------------------------------------------------------------------------
    void create_list2()
    {
    nod *q2;
    if(!p2)
    {
    
    p2=new nod;
    cout<<"re= ";
    cin>>p2->info.re;
    cout<<"im= ";
    cin>>p2->info.im;
    u2=p2;  
    }
    else 
    {
    
    q2=new nod; 
    cout<<"re= ";
    cin>>q2->info.re;
    cout<<"im=";
    cin>>q2->info.im;
    u2->next=q2;
    u2=q2;
    }
    u2->next=0;
    }
    //----------------------------------------------------------------------------
    void display_list1()
    {
    nod *q1;
    q1=p1;
    for(int i=1; i<=m1; i++)
    {
    for(int j=1; j<=n1; j++)
    if(q1!=0)
    {
    cout<<q1->info.re<<"+"<< q1->info.im;
    cout<<" ";
    q1=q1->next;
    }
    cout<<endl;
    }
    }
    //----------------------------------------------------------------------------
    void display_list2()
    {
    nod *q2;
    q2=p2;
    for(int i=1; i<=m2; i++)
    {
    for(int j=1; j<=n2; j++)
    if(q2!=0)
    {
    cout<<q2->info.re<<"+"<< q2->info.im;
    cout<<" ";
    q2=q2->next;
    }
    cout<<endl;
    }
    }
    //----------------------------------------------------------------------------
    void sum()
    {
    nod *q1, *q2, *q3;
    
    s=new nod;
    s->info.re=q1->info.re+q2->info.re;
    s->info.im=q1->info.im+q2->info.im;
    q1=p1;
    q2=p2;
    q3=s;
    while(q1!=u1 && q2!=u2)
    {
    q3=new nod;
    q3=q3->next;
    q1=q1->next;
    q2=q2->next;
    q3->info.re=0;
    q3->info.im=0;
    
    q3->info.re=q1->info.re+q2->info.re;
    q3->info.im=q1->info.im+q2->info.im;
    
    
    }
    u3=q3;
    
    }
    //----------------------------------------------------------------------------
    void display_sum()
    {
    nod *s;
    s=u3;
    for(int i=1; i<=m1; i++)
    {
    for(int j=1; j<=n1; j++)
    if(s!=0)
    {
    cout<<s->info.re<<"+" << s->info.im;
    cout<<" ";
    s=s->next;
    }
    cout<<endl;
    }
    }
    //----------------------------------------------------------------------------
    void main()
    {
    int i, j;
    cout<<"create first m: ";
    cout<<"n1=";
    cin>>n1;
    cout<<endl<<"m1="; cin>>m1;
    for(i=1; i<=n1; i++)
    for(j=1; j<=m1; j++)
    {
    create_list1();
    }
    display_list1();
    //----------------------------------------------------------------------------
    cout<<endl<<"create the second m: ";
    cout<<"n2=";
    cin>>n2;
    cout<<endl<<"m2="; cin>>m2;
    for(i=1; i<=n2; i++)
    for(j=1; j<=m2; j++)
    {
    create_list2();
    }
    display_list2();
    //----------------------------------------------------------------------------
    cout<<"sum: ";
    sum();
    
    display_sum();
    getch();
    }
    
     
    Last edited: Mar 9, 2012
  4. _nullptr

    _nullptr Major Geeky Geek Geek

    I thought I'd just throw together a small example that shows more clearly some
    simple things like displaying the list and freeing its memory. Hope it helps.
    Code:
    using namespace std;
    
    struct complex
    {
        double re;
        double im;
    };
    
    struct node
    {
        complex info;
        node*   next;
    };
    
    // function declarations
    void BuildList(int count);
    void PrintList(node* pListhead);
    void FreeList(node* pListhead);
    
    // initialize variables
    node* pListhead1 = NULL;
    node* pCurnode1  = NULL;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        for (int i = 1; i < 20; i++)
        {
            BuildList(i); // just a quick way to populate the list
        }
    
        PrintList(pListhead1);
        FreeList(pListhead1);
    
        cout << endl << "press Enter to exit" << endl;
        getchar();
    
    	return 0;
    }
    
    void BuildList(int count)
    {
        if (!pListhead1)
        {
            pCurnode1 = new node;
            pListhead1 = pCurnode1;
        }
        else
        {
            pCurnode1->next = new node;
            pCurnode1 = pCurnode1->next;
        }
    
        pCurnode1->info.im = count * 1.23;
        pCurnode1->info.re = count + 2.27;
        pCurnode1->next = NULL;
    
        return;
    }
    
    void PrintList(node* pListhead)
    {
        node* pCurnode = pListhead;
        if (!pListhead)
        { 
            return;
        }
    
        do
        {
            cout << "im = " << pCurnode->info.im << ", re = " << pCurnode->info.re << endl;
            pCurnode = pCurnode->next;
        } while (pCurnode); // traverse until node->next == NULL
    
        return;
    }
    
    void FreeList(node* pListhead)
    {
        node* pCurnode = NULL;
        node* pNextnode = NULL;
        if (!pListhead)
        {
            return;
        }
    
        pCurnode = pListhead;
        do
        {
            pNextnode = pCurnode->next; // save next node
            delete pCurnode;            // free memory
            pCurnode = pNextnode;
        } while (pCurnode); // until node->next == NULL
    
        return;
    }
    
     
  5. _nullptr

    _nullptr Major Geeky Geek Geek

    another example with a generic AddToList function + 2 lists
    Code:
    using namespace std;
    
    struct complex
    {
        double re;
        double im;
    };
    
    struct node
    {
        complex info;
        node*   next;
    };
    
    // function declarations
    
    // **** re: node** ppListhead in AddToList(...)
    // we need to pass the address of our pointer variable,
    // otherwise zero gets passed to the function, so memory allocated
    // never gets assigned to our pListhead variable
    
    void AddToList(node** ppListhead, double a, double b);
    void PrintList(node* pListhead);
    void FreeList(node* pListhead);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        node* pListhead1 = NULL;  // List 1
        node* pListhead2 = NULL;  // List 2
    
        for (int i = 0; i < 20; i++)
        {
            AddToList(&pListhead1, i*1.23, i + 2.27);   // List 1
            AddToList(&pListhead2, i + 9.343, i*9.343); // List 2
        }
    
        cout << "List 1" << endl;
        PrintList(pListhead1);
    
        cout << endl << "List 2" << endl;
        PrintList(pListhead2);
    
        FreeList(pListhead1);
        FreeList(pListhead2);
    
        cout << endl << "press Enter to exit" << endl;
        getchar();
    
        return 0;
    }
    
    void AddToList(node** ppListhead, double a, double b)
    {
        node* pCurnode = *ppListhead;
        if (!pCurnode)                 // if pListhead == NULL
        {
            pCurnode = new node;
            *ppListhead = pCurnode;
        }
        else
        {
            while (pCurnode->next)
            {
                pCurnode = pCurnode->next;
            }
    
            pCurnode->next = new node;
            pCurnode = pCurnode->next;
        }
    
        pCurnode->info.im = a;
        pCurnode->info.re = b;
        pCurnode->next = NULL;
    
        return;
    }
    
    void PrintList(node* pListhead)
    {
        node* pCurnode = pListhead;
        if (!pListhead)
        { 
            return;
        }
    
        do
        {
            cout << "im = " << pCurnode->info.im << ", re = " << pCurnode->info.re << endl;
            pCurnode = pCurnode->next;
        } while (pCurnode); // traverse until node->next == NULL
    
        return;
    }
    
    void FreeList(node* pListhead)
    {
        node* pCurnode = NULL;
        node* pNextnode = NULL;
        if (!pListhead)
        {
            return;
        }
    
        pCurnode = pListhead;
        do
        {
            pNextnode = pCurnode->next; // save next node
            delete pCurnode;            // free memory
            pCurnode = pNextnode;
        } while (pCurnode);
    
        return;
    }
    
     
  6. skyler43@123

    skyler43@123 Private E-2

    all right..i get the ideea;) thks man:)
    and i also resolved my problem:-D
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds