Assignment HELP!!

This is a discussion on Assignment HELP!! within the C Programming forums, part of the General Programming Boards category; Originally Posted by Adak Sure Tabstop - start the highly technical, complex arguments. What a pal you are!! No, it's ...

  1. #31
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484

    Wink

    Quote Originally Posted by Adak View Post
    Sure Tabstop - start the highly technical, complex arguments. What a pal you are!!
    No, it's not technical enough
    Make it like this
    Code:
    const char* szSayHi = "Hello, world!\n";
    printf(szSayHi);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #32
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLEN 200
        
    struct integer* read_integer(char* stringInt);
    void print(struct integer *p);
    struct integer* add(struct integer *p, struct integer *q);
    struct integer* subtract(struct integer *p, struct integer *q);
    int compare(struct integer *p, struct integer *q);
    
    struct integer{
        int *digits;
        int size;
    };
    
    int main(void){
        int numlines; //Number of lines stored in the file
        char string1[MAXLEN];
        char string2[MAXLEN];
        int i;// loop index
        int opr; //Operator 1 or 2 tells us if we add or subtract
        struct integer *p;
        struct integer *q;
        struct integer *answer;
    
        // Open the file and read from bigint.txt
        FILE *fin;
        fin = fopen("bigint.txt","r");
        fscanf(fin, "%d", &numlines); //Read in how many lines there are in the file
    
         // read a line (\n)
        // read the operator(1 or 2) - forget about this one. 3 fscanf
        //scanf first sting; call read int (1st) function
        //same with the second string
        //if 1 then call add, if 2 then call subtract
        // end of loop
        for(i = 0; i < numlines; i++)
        {
            fscanf(fin, "%d", &opr);  // find out whether its add or subtract
            fscanf(fin, "%s", string1); // scan in 2nd number on the first line of input file
                p = read_integer(string1); 
            fscanf(fin, "%s", string2);// scan in 3rd number on the first line of input file
                q = read_integer(string2);
        
            print(p);//call function to print out information
            printf(" ");
            print(q);
            printf("\n");
    
            if(opr == 1)
            {
             //add
              
              answer = add(p, q);
              print(answer = add(p, q));
            }
            else if(opr == 2)
            {
                printf("subtract here\n");
             // subtract func  
            }
        }
        
    
            
        fclose(fin);
        system("PAUSE");
        return 0;
    }
    
    //Preconditions: the first parameter is string that stores
    //               only contains digits, doesn't start with
    //               0, and is 200 or fewer characters long.
    //Postconditions: The function will read the digits of the
    //	large integer character by character, 
    //	convert them into integers and return a 
    //             pointer to the appropriate struct integer.
    struct integer* read_integer(char* stringInt)
    {
        int len;
        struct integer *myString;//create a new struct
        int i;//loop index
        //allocate memory for the struct
        myString = (struct integer*)(malloc(sizeof(struct integer)));
        
        len = strlen(stringInt); // length of the strinInt in digits
        
        myString->size = len; // length of string assigned to size in struct myString
        
        //allocate memory for digits
        myString->digits = (int*)(malloc(sizeof(int)* len));
        
        // assign number to digits, convert to integers
        for(i = 0; i < len; i++)
        {
            myString->digits[i] = stringInt [len-1-i] - '0';
        }
        return myString;
    }
    
    //Preconditions: p is a pointer to a big integer.
    //Postconditions: The big integer pointed to by p is 
    //                printed out.
    void print(struct integer *p)
    {
    //given pointer, want to print out digits in reverse order...
        int i;
        // no reversing in add or subtract, but want to reverse here
        for(i = p->size-1; i >= 0; i--)
        {
            printf("%d", p->digits[i]);
        }
        
    }
    //Preconditions: p and q are pointers to struct integers.
    //Postconditions: A new struct integer is created that 
    //                stores the sum of the integers pointed to 
    //                by p and q and a pointer to it is 
    //                returned.
    struct integer* add(struct integer *p, struct integer *q)
    {
        int i;
        struct integer *addAnsw; //create new struct for answer
        
        //Allocate memory for the new struct
        addAnsw = (struct integer*)(malloc(sizeof(struct integer)));
        
        //Allocate memory for the digits in the new struct addAnsw
        addAnsw->digits = (int*)(malloc(sizeof(int)* (p->size)));
        
      for(i = 1;i <2 i++)
        {
            printf("%d + %d = %d\n", p->digits[i], q->digits[i], addAnsw->digits[i]);
             addAnsw->digits[i] = p->digits[i] + q->digits[i];
        }
        
        return addAnsw;
    }
    
    //Preconditions: p and q are pointers to struct integers.
    //Postconditions: A new struct integer is created that 
    //                stores the absolute value of the 
    //                difference between the two and a pointer 
    //                to this is returned.
    struct integer* subtract(struct integer *p, struct integer *q)
    {
           struct integer *subtractAnsw;
           
          //Allocate memory for the new struct
          subtractAnsw = (struct integer*)(malloc(sizeof(struct integer)));
          
           if(p->size < q->size)
           {
                subtractAnsw->digits = (int*)(malloc(sizeof(int)*(p->size)));
           }   
    }
    
    //Preconditions: Both parameters of the function are 
    //	  pointers to struct integer. 
    //Postconditions: The function compares the digits of two 
    //	numbers and returns: 
    //    -1 if the first number is smaller than the second, 
    //     0 if the first number is equal to the second number,
    //   1 if the first number is greater than the second.

  3. #33
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    answer = add(p, q);
    print(answer = add(p, q));
    you are calling add twice - so the first result is lost - you hav a memory loss here

    of cource - while you loop - you do not free p,q and answer as well - which generates 3 more losses on each iteration
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #34
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    ok but that doesnt help with the add answer..

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What has you confused about adding the two digits? I don't understand.

  6. #36
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    it wont print out the numbers correctly,

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21