logo
Tags down

shadow

Trying to do this homework assignment but I keep getting a compilation error


By : user3852534
Date : October 14 2020, 11:12 PM
Hope this helps Declare "j" outside the "for" loop. When you declare it inside the loop header, it's local to the loop's block and not visible outside it.
code :


Share : facebook icon twitter icon

Not so Useful Error -- Segmentation Fault (Core Dump) in Homework Assignment


By : Davie Smith
Date : March 29 2020, 07:55 AM
With these it helps compiling it with gcc gives this:
test.c: In function ‘read_integer’: test.c:165: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast /usr/include/stdlib.h:148: note: expected ‘const char *’ but argument is of type ‘char’
code :
    tok = strtok( stringInt, NULL ); // Tokenize the string by each char
    n = atoi( tok[i] ); // Convert char to an integer
   n =  stringInt - '0'; // Convert char to an integer
/*
 * File:   struct integer.c

 * Description: Assignment in using Linked Lists
 * in order to perform struct integer addition and subtraction.
 *
 * Created on September 1, 2010, 11:38 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// Constant Definitions
#define ADD 1
#define SUB 2

// Output file
char *fName = "out.txt";
FILE *outFile;


/*
 * Create a prototype of a single
 * node in the Linked List.
 * Each node will represent a single
 * integer comprising one part of a struct integer.
 */
struct integer
{
    int digit;

    struct integer *next;
};


// Function Prototypes
struct integer* read_integer( char *stringInt );
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 );

void print( struct integer *p );


// Main function
int main( )
{
    //Variable initialization

    /*
     * Initialize pointers to the linked lists.
     * One, *head, will always point to the
     * first element, the head, of the list.
     * The other element, *curr, will point to the
     * node currently being accessed, and will be
     * used to traverse the list.
     */
    struct integer* pHead;
    struct integer* qHead;
    struct integer* tHead; // Used to contain the total

    int numOps, oper, i;
    const char *fileName = "struct_integer.txt";
    char bigintstr[200];
    FILE *inputFile;

    // Open output file
    outFile = fopen(fName, "a+");


    // Open up the input file for reading
    inputFile = fopen(fileName, "r");

    // Read in the number of operations to be performed
    fscanf(inputFile, "%d", &numOps);

    /*
     * For each operation that must be performed,
     * construct a linked list for each of the
     * struct integers in the file. Then, perform the operation
     * indicated by the digit preceding them.
     */
    for( i = 0; i < numOps; i++ )
    {
        // Read in the number that dictates operation
        fscanf(inputFile, "%d", &oper);

        // Read in the first struct integer into a string
        fscanf(inputFile, "%s", bigintstr);

        /*
         * Pass the struct integer string to read_integer()
         * in order to construct a linked list out of it.
         */
        pHead = read_integer( bigintstr );

        // Read in second struct integer into a string
        fscanf(inputFile, "%s", bigintstr);

        /*
         * Pass the struct integer str to read_integer()
         * in order to construct a linked list out of it.
         */
         qHead = read_integer( bigintstr );

         /*
          * Depending on the operation to be performed,
          * call the corresponding function.
          */
         switch( oper )
         {
             case ADD:
                 tHead = add( pHead, qHead );
                 break;
             case SUB:
                 tHead = subtract( pHead, qHead );
                 break;
             default:
                 printf("Invalid operation parameter.\n");
         }

         print( pHead ); // Print out the first struct integer
         fprintf(outFile, " + ");
         print( qHead ); // Print out the second struct integer
         fprintf(outFile, " = ");
         print( tHead ); // Print out the sum/difference struct integer
         fprintf(outFile, "\n"); // Move to next line for next instruction set
    }
    fclose(inputFile);
    fclose(outFile);
    //system(PAUSE);
    return 0;
}


// Function Definitions

/*
 * Function read_integer
 *
 * @Parameter CHAR* stringInt
 *
 * Parameter contains a string representing a struct integer.
 * Tokenizes the string by each character, converts each char
 * into an integer, and constructs a backwards linked list out
 * of the digits.
 *
 * @Return STRUCT* Integer
 */
struct integer* read_integer( char* stringInt )
{
    int i, n;
    struct integer *curr, *head;

    int numDigits = strlen( stringInt ); // Find the length of the struct integer
    char *tok;
    head = NULL;

    for( i = 0; i < numDigits; i++ )
    {
        n =  stringInt[i] - '0'; // Convert char to an integer

        curr = (struct integer *) malloc (sizeof( struct integer )); // Allocate memory for node
        curr->digit = n; // Digit of current node is assigned to n
        curr->next = head; // Move to the next node in the list.
        head = curr; // Move head up to the front of the list.
    }

    return head; // Return a pointer to the first node in the list.
}

void reverse (struct integer **p){
    if((*p)->next==0) return;
    struct integer *i=*p,*j;
    while(i->next){
        j=i;
        i=i->next;
    }//i is now the tail;
    i->next=j;
    j->next=0;
    reverse(p);
    *p=i;
}

/*
 * Function print
 *
 * @Parameter STRUCT* Integer
 *
 * Given a linked list, will traverse through
 * the nodes and print out, one at a time,
 * the digits comprising the struct integer that the
 * linked list represents.
 *
 * TODO: Print to file
 */
void print( struct integer *p )
{
    struct integer *head=p;
    reverse(&p);    
    while( p )
    {
        fprintf(outFile, "%d", p->digit);
        p = p->next;
    }
    reverse(&head);
}


/*
 * Function add
 *
 * @Paramater STRUCT* Integer
 * @Parameter STRUCT* Integer
 *
 * Takes two linked lists representing
 * big integers stored in reversed order,
 * and returns a linked list containing
 * the sum of the two integers.
 *
 * @Return STRUCT* Integer
 * 
 * TODO Comment me
 */
struct integer* add( struct integer *p, struct integer *q )
{
    int carry = 0;

    struct integer *sHead, *sCurr;
    struct integer *pHead, *qHead;

    pHead = p;
    qHead = q;

    sHead = NULL;

    while( p )
    {
        sCurr = ( struct integer* ) malloc (sizeof(struct integer));
        sCurr->digit = p->digit + q->digit + carry;
        sCurr->next = sHead;
        sHead = sCurr;

        carry = 0;

        /*
         * If the current digits sum to greater than 9,
         * create a carry value and replace the current
         * value with value mod 10.
         */
        if( sCurr->digit > 9 )
        {
            carry = 1;
            sCurr->digit = sCurr->digit % 10;
        }

        /*
         * If the most significant digits of the numbers
         * sum to 10 or greater, create an extra node
         * at the end of the sum list and assign it the
         * value of 1.
         */
        if( carry == 1 && sCurr->next == NULL )
        {
            struct integer *sCarry = ( struct integer* ) malloc (sizeof(struct integer));
            sCarry->digit = 1;
            sCarry->next = NULL;
            sCurr->next = sCarry;
        }

        p = p->next;
        q = q->next;
    }
    reverse(&sHead);
    return sHead;
}



/*
 * Function subtract
 *
 * @Parameter STRUCT* Integer
 * @Parameter STRUCT* Integer
 *
 * Takes two linked lists representing struct integers.
 * Traverses through the lists, subtracting each
 * digits from the subsequent nodes to form a new
 * struct integer, and then returns the newly formed
 * linked list.
 *
 * @Return STRUCT* Integer
 * 
 * TODO Comment me
 */
struct integer* subtract( struct integer *p, struct integer *q )
{
    int carry = 0;

    struct integer *dHead, *dCurr;
    struct integer *pHead, *qHead;

    pHead = p;
    qHead = q;

    dHead = NULL;

    while( p )
    {
        dCurr = (struct integer*) malloc (sizeof(struct integer));
        dCurr->digit = p->digit - q->digit - carry;
        dCurr->next = dHead;
        dHead = dCurr;

        if( dCurr->digit < 0 )
        {
            dCurr->digit += 10;
            carry = 1;
        }

        if( dCurr->next == NULL && carry == 1 )
        {
            struct integer *dCarry = (struct integer*) malloc (sizeof(struct integer));
            dCarry->digit = -1;
            dCarry->next = NULL;
            dCurr->next = dCarry;
        }

        p = p->next;
        q = q->next;
    }

    return dHead;
}

/*
 * Function compare
 *
 * @Parameter STRUCT* Integer
 * @Parameter STRUCT* Integer
 *
 * Takes in two linked lists representing struct integers.
 * Traverses the lists one at a time, comparing the
 * digits.
 *
 * Case: p < q
 * @Return -1
 *
 * Case: p == q
 * @Return 0
 *
 * Case: p > q
 * @Return 1
 * 
 * TODO Comment me
 */
int compare( struct integer *p, struct integer *q )
{
    struct integer *pHead, *qHead;
    int comp = 0;

    pHead = p;
    qHead = q;

    while( p )
    {
        if( p->digit > q->digit )
        {
            comp = 1;
        }

        if( p->digit < q->digit )
        {
            comp = -1;
        }

        p = p->next;
        q = q->next;
    }

    return comp;
}
void reverse (struct integer **p){
    if((*p)->next==0) return;
    struct integer *i=*p,*j;
    while(i->next){
        j=i;
        i=i->next;
    }//i is now the tail;
    // 1->2->3->4->NIL

    i->next=j;
    // 1 -> 2 -> 3 <-> 4

    j->next=0;
    // 1 -> 3 -> 3 <- 4
    //           |
    //           v
    //          NIL
    reverse(p);// p looks like 1 -> 2 -> 3 now recurse
    *p=i;
}

Java compiler error - homework assignment


By : user2983507
Date : March 29 2020, 07:55 AM
wish of those help The compiler gives me the error "'void' type not allowed here...<= operator cannot be applied to java.lang.String,int...not a statement" , Let's just look at this statement:
code :
(yourClock.getHours()) <= 9 ? ".0" : ".")
((yourClock.getHours() <= 9) ? ".0" : ".")

Why is an assignment to a base class valid, but an assignment to a derived class a compilation error?


By : Andre Cardoso
Date : March 29 2020, 07:55 AM
will be helpful for those in need Because the implicitly declared copy assignment operator of B hides the implicitly declared copy assignment operator of A.
So for the line b = a, only the the operator= of B is a candidate. But its parameter has type B const&, which cannot be initialized by an A argument (you would need a downcast). So you get an error.

Compilation error with assignment expression


By : Niole Nelson
Date : March 29 2020, 07:55 AM
To fix this issue You are attempting to use the return of the get method as a variable. Unlike array access expressions, e.g. arr[i] = value, which are legal, the results of method calls cannot be used this way. You must use the set method instead.

Python Attribute Error in homework assignment


By : Bhagirath Vaghela
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am running the following code which is copied directly from my Starting out with Python book. I keep getting the following error: , Your indentation is incorrect. It should look like this:
Related Posts Related Posts :
  • how to use dll?
  • C++ string literal data type storage
  • How to get qmake to generate "project dependencies" in a Visual Studio .sln project
  • Why would someone use C instead of C++?
  • C ReadProcessMemory - how to examine the memory area associated with a process
  • how can I force C++ macro substitution at the time I choose in this case?
  • C++: What is the appropriate use for the std::logic_error exception?
  • Is there a working C++ grammar file for ANTLR?
  • Copy Constructor?
  • How to start a voice quality PESQ test?
  • I'm trying to change my Maze traversal recursive coding part into a while loop
  • Providing less than operator for one element of a pair
  • Question about exact time of destruction of temporaries in C++
  • Please suggest some algorithm to find the node in a tree whose distance to its farthest node is minimum among all the no
  • Best practices with object manager
  • Using input to call a member function
  • How to find out if a character in a string is an integer
  • How to find a formatted number in a string?
  • Multiple Counter Problem In For Loop
  • Protecting /etc/passwd and /etc/shadow from concurrent access
  • Getting ptr from memory address with c++
  • How to make the Win32 APP Background Transparent?
  • How do you return a 2d array in C++?
  • static member initialization for specialized template class
  • How to find the location of two consecutive spaces in a string
  • c++ volatile multithreading variables
  • What is the difference between an array and a dynamic array?
  • Why is the Loki library not more widely used?
  • Corruption of the heap & F12 Problem
  • Colors in C++ win32 console
  • Position of elements in vector
  • How to convert an ASCII string to an UTF8 string in C++?
  • warning: GDB: Failed to set controlling terminal: Operation not permitted
  • How to find the first character in a C++ string
  • How to test if a string contains any digits in C++
  • C++ templated class implementation of the multiton pattern
  • C++ - Two Basic Questions
  • Finding all permutations that match a set of rules
  • How to get data out of the STL's const_iterator?
  • How can I make a banner in QT, like the news banner of CNN/FOX?
  • Various ways to get month name from inbuilt C library
  • How do I programmatically send email w/attachment to a known recipient using MAPI in C++? MAPISendMail()
  • Why is there no boost::copy_on_write_ptr?
  • How to create a radio button and see if it's checked?
  • Simple expression parser example using Boost::Spirit?
  • g++, R_X86_64_32S : what is it?
  • Find the numbers missing
  • What is wrong with my code? My program will not compile
  • Undefined references when including boost library
  • Avoiding Calls to floor()
  • C++ Need to compare one string to 200.000 words
  • Display web page using libgtkhtml c c++
  • wxWidgets and context menus
  • How to get PCM data from microphone in C++ (os Windows)?
  • Header file best practices for typedefs
  • Calling a method with this pointer from an inherited class becomes const
  • C socket API is thread safe?
  • Can a class member function template be virtual?
  • How do I get a string description of a Win32 crash while in Top level filter (I am looking for the address of the instru
  • Pcrepp - Perl Regular Expression syntax to match host name
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org