Category: C++

  • Create me a Program using SFML and C++ of Tic Tac Toe

    Using this pdf file and the following code, add a simple function running this game perfectly fine but being slightly different for a beginner programmer.

  • Help fixing logical error and segmentation fault in C++ link…

    I’m currently working on a C++ project involving [sebutkan topiknya, misal: Dynamic Memory/Arrays]. My code compiles, but I keep getting a runtime error (segmentation fault) when trying to execute the function.

    I need a tutor to:

    1. Identify the cause of the memory leak/error.
    2. Explain the fix clearly so I can avoid this in the future.
    3. Ensure the code follows best practices (using smart pointers if applicable).

    Attached is my current source code.

  • Project 2: myELIZA

    For this project, you and your group will be designing and implementing a small system in C or C++ that will simulate a primitive AI chatbot. Specifically, you will be implementing a version of ELIZA that is one of the earliest natural language processing programs. The program will simulate a conversation between the user and a psychiatrist. You should consider the program interaction as the user having an initial psychiatric interview.

    Details:

    • Your program will be based on C++ regular expressions that will be used for pattern matching and substitution methodology that gave users an illusion of understanding. You need to watch the video to understand the basic expectations of the program, and possible algorithms and data structures that could be implemented for this project.
    • Further, you will need to utilize the . You may access it from the website (), the terminal, or an IDE of your choice. Use
    • Also, as a reminder, all of the work for this project must be the sole product of the group. You may not share code with other groups, or download solutions off the internet, as doing so will be considered cheating. If you are struggling with a concept or implementation, you are encouraged to contact the instructor or your TAs for aid.

    Requirements

    This assignment has two parts: a wiki portion and an implementation portion.

    Wiki

    For the wiki portion, the group must generate documentation describing their system using the wiki functionality provided by GitLab. The wiki must contain at least one page that provides a high-level description of the entire system and must contain at least one page for each of the major functionality components (i.e. if there are six major functionality components, there should be at least six pages).

    For the high-level description page, the team must provide a brief description of each of the major functionality components, how they work together, and some of the major data structures across the entire project. This information should not be copied and pasted from the project instructions. The page must also contain a diagram of the entire system, based on the one created during recitations. The diagram must be created digitally (i.e. using PowerPoint, Photoshop, Paint.net, UMLet, etc.), must be easy to read and understand, and cannot a photographed or scanned image.

    For each major functionality component page, the student accountable for that component must provide a detailed description of their component. This description should have three labeled sections: a brief description of the purpose of the component, a description of how data was stored and maintained for this component, and a description of the functionality for the component. They might also consider including diagrams to more easily visualize how all of the pieces fit together.

    For the data storage and maintenance section, there should be an explanation of how data was stored and maintained in their component. What, if any, objects or structs were created to store data? How were they organized and managed? What types of formal data structures did were made use of (trees, graphs, arrays, hashes, etc)?

    For the functionality component, there should be an explanation of the major functions in the component. How is data moved and transformed? How is it read in? How is it output? What are the various major functions constructed and how do they work?

    Descriptions and explanations should be primarily in prose and paragraph format, not bulleted lists. Code snippets are also acceptable, but must be used as an enhancement to the explanation of functionality not as a substitution for it.

    Your grade for the wiki will partly be based on apparent effort, so please be thorough in your descriptions. Additionally, because this is a wiki, the high-level description page must have links to all of the major functionality component pages.

    Implementation

    Your program must provide the following functionality and adhere to the following constraints:

    • Your int main() should be in its own .c/.cpp file
    • All .c/.cpp files, except your c/main.cpp, must have associated header files. You may not #include a .c/.cpp file, only header files
    • The user will use his or her natural language to interact with the system. This means that there is no specific format required from the user or menu with options that need to be selected from the user to get a response from the system. However, for this assignment, assume that the user is communicating with single sentences, common phrases or short answers like “yes”, “I do not know”, “maybe”, etc.
    • Your system should respond to every user input even if invalid or non understandable :
      • Read in and store answers to keep track of repeated responses to motivate the user to give a different response.
      • End the program when the users types “bye”.
      • Have at least 50 different type of regular expressions to match responses and respond accordingly.
      • Your program must use regex to define regular expressions and then regex_match(), regex_search(), and regex_replace() to elaborate system responses.
    • See the example of .
    • Review the content of the following Webpages as resources: , and .
    • Major functionality components must be constructed in some function, or across some functions, that are declared and defined outside of your c/main.cpp . Remember, function declarations must be stored in a header file, while definitions must be stored in a .c/.cpp file. You may have additional functions that support your major functionality component function.
    • Your code must be well commented.
    • Each group member should be performing regular commits to the GitLab repository server with meaningful commit Fixed bug or New code are not meaningful, so try to be more specific about what was fixed or what was added.
    • You must provide a .txt format README file which includes:
      • The names of all group members
      • Instructions on how to compile your program
      • Instructions on how to run your program
    • Additionally, you may write a makefile, but please specify if it needs any additional flags to function properly.

    Each student must be accountable for one or more major functionality components, and may not swap after they sign up for a component barring an exceptional circumstance. Failure to be accountable for any major functionality component will result in a 0 for the coding portions of the project (milestone submission and/or final submission). Keep in mind that some components build on others, so be careful about who takes ownership of which pieces and manage your time to avoid a crunch near the due date. Also, the group should strive to balance the work across all members.

    The major functionality components are responses to user input that can be classified as containing any of the following:

    1. Generic verbs such have, purchase, travel, know, learn, etc.
    2. Love or relationships (keywords like love, spouse,girlfriend,boyfriend, etc)
    3. Concern ( keywords like hate, kill, suicide, etc.)
    4. Financial ( keywords like money, savings, investments, bankruptcy, etc.)
    5. Wellbeing (keywords like exercise, vitamins, relaxing, sleeping, etc).
    6. Education (keywords like courses, university, studying, student, education, etc. )
    7. Entertainment ( keywords like concert, movies,dancing, restaurant,bar, etc. )
    8. Others defined by group

    Milestone Project Submission

    Your program must provide all requested functionality for major functionality component 1, and storing user responses to detect multiple identical answers from the user and motivate to get a different response. At least one group member must submit a .zip file containing the following:

    1. All files necessary to compile and run your program (Please do not include any files not necessary to run the program on the CSE machines)
    2. A .txt format README file explaining how to compile and run your program

    Final Project Submission

    Your program must provide all requested functionality. At least one group member must submit a .zip file containing the following:

    1. All files necessary to compile and run your program (Please do not include any files not necessary to run the program on the CSE machines)
    2. A .txt format README file explaining how to compile and run your program
  • C++ Question

    in c++ code, the zip file is what it being asked, need a client.cpp file and a server.cpp file. below has some code example given that can be used but needs to be commented “code from given file”

    Objective:

    This assignment will introduce you to interprocess communication mechanisms in UNIX using sockets.

    Problem:

    You must write two programs to implement a distributed version of the multithreaded program you created for programming assignment 1. These programs should find the positions of the symbols in the original message, reconstruct the original message (based on the encoded message), and determine the number of bits required to represent the positions using Elias-Gamma encoding.

    These programs are:

    The server program:

    The user will execute this program using the following syntax:

    ./exec_filename port_no

    where exec_filename is the name of your executable file, and port_no is the port number to create the socket. The port number will be available to the server program as a command-line argument.

    The server program receives no input from STDIN and prints no messages to STDOUT.

    The server program executes the following task:

    • Receive multiple requests from the client program using sockets. Therefore, the server program creates a child process for each request to handle them simultaneously. For this reason, the parent process must handle zombie processes by implementing the fireman() function call (unless you can determine the number of requests the server program receives from the client program).

    Each child process executes the following tasks:

      1. Receive the encoded message, the assigned symbol, and the sum of the frequencies for the symbols assigned to the previous threads on the client program.

      2. Determines the positions of the assigned symbol in the original message.

      3. Calculates the number of bits used to represent the symbol’s position using Elias Gamma encoding.

      4. Send the positions and the number of bits used to represent the symbol’s positions to the client program using sockets.

    The client program:

    The user will execute this program using the following syntax:

    ./exec_filename hostname port_no < input_filename

    where exec_filename is the name of your executable file, hostname is the address where the server program is located, port_no is the port number used by the server program, and input_filename is the name of the input file. The hostname and the port number will be available to the client as command-line arguments.

    The client program receives multiline input in accordance with the specifications for Programming Assignment 1.

    Input example:

    4
    A 5
    C 2
    B 2
    D 3
    10100010000010000001010001100011100010010010100010110110001100

    After reading the information from STDIN, the program must create n child threads (where n is the alphabet size). Each child thread executes the following tasks:

    1. Receives the encoded message, the symbol to decode, and the information needed to find the positions of the assigned symbol in the encoded message.

    2. Send the received information to the server program using sockets.

    3. Receive the positions and number of bits used to represent the symbol’s positions from the server program using sockets.

    4. Store the received information in a memory location accessible by the main thread.

    5. Stores the assigned symbol, using the determined positions, into the memory address shared with the main thread that contains the decoded message.

    Finally, the main thread prints the information for each symbol to STDOUT (sorted according to the compression algorithm rules) and the decoded message.

    The expected output (given the input above) is:

     

    Symbol: A, Frequency: 5
    Positions: 0 1 3 7 9
    Bits to represent the position(s): 23

    Symbol: D, Frequency: 3
    Positions: 5 6 8
    Bits to represent the position(s): 17

    Symbol: B, Frequency: 2
    Positions: 4 10
    Bits to represent the position(s): 12

    Symbol: C, Frequency: 2
    Positions: 2 11
    Bits to represent the position(s): 10

    Decoded message: AACABDDADABC

    Notes:

     
    • The position values for the symbols must start from 1 (not 0), since Elias Gamma can only represent positive integers starting from 1.
    • You can safely assume that the input will always be in the proper format.
    • You must use the output statement format based on the example above.
    • For the client program, you must use POSIX Threads and stream sockets. A penalty of 100% will be applied to submissions not using POSIX Threads and Stream Sockets.
    • For the server program, you must use multiple processes (fork) and stream sockets. Submissions that do not use multiple processes and Stream Sockets will be penalized by 100%.
    • The Moodle server will kill your server program after executing each test case.
    • You must present code that is readable and has comments explaining the logic of your solution. A 10% penalty will be applied to submissions that do not follow this guideline.
    • You cannot use global variables. A 100% penalty will be applied to submissions using global variables.
    • A 100% penalty will be applied to solutions that do not compile.
    • A 100% penalty will be applied to solutions that hardcode the output.

    RUBRIC:

    • 10 points for each test case (20 points total)
    • 10 points for presenting clean and readable code.
    • 10 points for using the fireman function correctly, or a for loop based on the number of requests.
    • 10 points for reading information from STDIN (client).
    • 25 points if each request to the server:
      • Receives information from the client program and creates a child process per request (5 points).
      • Generates the request information (positions and the number of bits used to represent them with Elias Gamma). (10 points).
      • Return the information to the client program (5 points).
      • Closes the socket (5 points).
    • 25 points if each child thread:
      • Creates a socket to communicate with the server (5 points).
      • Sends and receives information to/from the server (10 points).
      • Stores the information in a memory location accessible by the main thread (5 points).
      • Closes the socket (5 points).

    ADDITIONAL PENALTIES:

    100 points off for a solution not compiling (in addition to test cases not passing)

    100 points off for lack of generalized solution (hard-coding, no server file, etc.)

    100 points off for not using POSIX threads, fork(), or Sockets.

    100 points for using global variables.

    // Hostname = 172.25.215.151, port = 3360

    /*

    struct student

    {

    char name[20];

    int psid;

    int grade;

    };

    */

    this is an example of a server.cpp file

    #include <unistd.h>

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <iostream>

    #include <sys/types.h>

    #include <sys/socket.h>

    #include <netinet/in.h>

    struct student

    {

    char name[20];

    int psid;

    int grade;

    };

    // Fireman function

    void fireman(int)

    {

    while (waitpid(-1, NULL, WNOHANG) > 0);

    }

    int main(int argc, char *argv[])

    {

    int sockfd, newsockfd, portno, clilen;

    struct sockaddr_in serv_addr, cli_addr;

    int n;

    // Creating a signal to execute the fireman function when a child process end its execution

    signal(SIGCHLD, fireman);

    // Check for the port number from the command line

    if (argc < 2)

    {

    std::cerr << “ERROR, no port providedn”;

    exit(1);

    }

    //Create a socket descriptor

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)

    {

    std::cerr << “ERROR opening socket”;

    exit(1);

    }

    //Populate the sockaddr_in structure

    bzero((char *)&serv_addr, sizeof(serv_addr));

    portno = atoi(argv[1]);

    serv_addr.sin_family = AF_INET;

    serv_addr.sin_addr.s_addr = INADDR_ANY;

    serv_addr.sin_port = htons(portno);

    // Bind the socket descriptor with the sockaddr_in structure

    if (bind(sockfd, (struct sockaddr *)&serv_addr,

    sizeof(serv_addr)) < 0)

    {

    std::cerr << “ERROR on binding”;

    exit(1);

    }

    // Set the maximum number of concurrent connections

    listen(sockfd, 20);

    clilen = sizeof(cli_addr);

    while (true)

    {

    // Accept a request from the client. A new socket descriptor is created to handle the request

    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);

    // Create a child process to answer the request.

    if (fork() == 0)

    {

    if (newsockfd < 0)

    {

    std::cerr << “ERROR on accept”;

    exit(1);

    }

    student s;

    n = read(newsockfd, &s, sizeof(student));

    if (n < 0)

    {

    std::cerr << “ERROR reading from socket”;

    exit(1);

    }

    std::cout << “Name: ” << s.name << std::endl;

    std::cout << “PSID: ” << s.psid << std::endl;

    std::cout << “Grade: ” << s.grade << std::endl;

    //Close the newsocket descriptor

    close(newsockfd);

    // Terminate the child process

    _exit(0);

    }

    }

    close(sockfd);

    return 0;

    }

    and below is example of client.cpp

    #include <unistd.h>

    #include <iostream>

    #include <string.h>

    #include <stdlib.h>

    #include <sys/types.h>

    #include <sys/socket.h>

    #include <netinet/in.h>

    #include <netdb.h>

    struct student

    {

    char name[20];

    int psid;

    int grade;

    };

    int main(int argc, char *argv[])

    {

    int sockfd, portno, n;

    struct sockaddr_in serv_addr;

    struct hostent *server;

    // Checking for the hostname and port number from the command line.

    if (argc < 3)

    {

    std::cerr << “usage ” << argv[0] << “hostname portn”;

    exit(0);

    }

    // Creating a socket descriptor

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)

    std::cerr << “ERROR opening socket”;

    // Storing the hostname information

    server = gethostbyname(argv[1]);

    if (server == NULL)

    {

    std::cerr << “ERROR, no such hostn”;

    exit(0);

    }

    // Transforming the port number to int

    portno = atoi(argv[2]);

    // Populating the sockaddr_in structure

    bzero((char *)&serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;

    bcopy((char *)server->h_addr,

    (char *)&serv_addr.sin_addr.s_addr,

    server->h_length);

    serv_addr.sin_port = htons(portno);

    // Connecting to the server

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)

    {

    std::cerr << “ERROR connecting”;

    exit(1);

    }

    // Sending a symbol

    student s = {“Carlos Rincon”,1234,100};

    n = write(sockfd, &s, sizeof(student));

    if (n < 0)

    {

    std::cerr << “ERROR writing to socket”;

    exit(1);

    }

    // Closing the socket

    close(sockfd);

    return 0;

    }

  • DISCUSSION QUESTION

    Arrays and Vectors Discussions

    TOPIC: Should beginners focus solely on learning std::vector first, or is a deep understanding of C-style arrays and manual memory management a necessary foundation for all C++ programmers?

    You must post before seeing replies

    MAIN POST: After reading the materials for this week and conducting any necessary independent research, write your summary according to all discussion requirements for the course. Use APA formatting for any resource reference and in-text citations as instructed in the course information standards.

    RESPONSES: All responses need to be specific in expressing your insight and clear when posing any additional questions extending the discussion. A minimum of 50 words per response is required. Responses must support, engage, and extend the discussed topics.

  • What is c++?

    C++ is a programming language.

  • Computer science 2 Homework

    Please read Homework 2 document and sample output carefully, and submit the required files (source code files, a makefile, and a design document). Please see the sample design document.

  • C++ Question

    Week 2 Assignment: Stack Implementation Using Arrays and Linked Lists

    Implement the stack data structure using both arrays and linked lists in C++.

    Tasks:

    • Implement a stack using an array. Include functions for push, pop, and top operations.
    • Implement a stack using a linked list. Include functions for push, pop, and top operations.
    • Write a main function to test both implementations with various operations.
    • Compare the time complexity of push, pop, and top operations in both implementations.
    • Discuss the advantages and disadvantages of using arrays vs. linked lists for implementing a stack.

    For this C++ assignment, submit a word document or PDF with screenshots of the working code snippets and the successful output of the program.