Google Answers Logo
View Question
 
Q: Visual c++ problem ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Visual c++ problem
Category: Computers > Programming
Asked by: purplepit-ga
List Price: $25.00
Posted: 11 May 2003 14:34 PDT
Expires: 10 Jun 2003 14:34 PDT
Question ID: 202426
Can you please write a (cpp) program that runs without errors,
according to the following specification......

Write a short demo program which constructs a queue and a priority
queue, each to store strings. Enter 10 names (from the keyboard) and
add each name to both queues. Display both queues (as a 2 column
table). Confirm the results are as expected.

Request for Question Clarification by jeanluis-ga on 12 May 2003 05:28 PDT
Question: For the priority queue, how do we determine the priority of
each name? I assume the range for the priorities is 0-9 (maybe 1-10?)
zero being higest priority. Please correct me if I am wrong.
Thanks,
--jld

Clarification of Question by purplepit-ga on 12 May 2003 07:29 PDT
Hi there,
Yes 1-10 will be ok!! and 0 would be the highest priority...
Thanks
Purpleit-ga

Request for Question Clarification by jeanluis-ga on 12 May 2003 08:13 PDT
Opps, I am sorry, I guess my request for clarification wasn't very
clear  <grin>
Really I wanted to know the range for the priority, which I got, 1-10
with 1 being the higest (because zero isn't a valid priority if the
range is 1-10...)
But I also wanted to know how do we determine the priority, typically
the user assigns priority. I could assume a priority based on order of
input, or I could assign priority randomly. But usually the user would
assign a priority, for example you might enter the following input:
name1, 10
name2, 5
name3, 8
etc...

Then the program basically outputs the queue as the name list in order
that they were entered (i.e. FIFO) and the priority queue would be
listed according to priority. So let me know how to determine priority
of each name...

Another issue that comes up, (which may be beyond the scope of this
program) is time to process, and time of entry into the queue. For
example if you entered name1 at time t=0 and it takes 2 time units to
process with a priority 5 then you enter name2 at t=3 with a priority
of 1 then even though name2 has higher priority name1 actually gets
processed first because it was in the queue first and was processed
before name2 even entered the queue.... Anyhow I assume that time is
not a factor and that all names should be processed on priority only.
(Unless you say otherwise.)
Let me know 
--jld

Clarification of Question by purplepit-ga on 12 May 2003 09:19 PDT
Hi again,
Thinking about this, I only really have the question that was
origionally posted to go on!!!!
So I'll leave it up to you to do it your way, but I guess the 'user'
method would be favourite....
Purplepit-ga

Request for Question Clarification by jeanluis-ga on 12 May 2003 10:12 PDT
Ok this sounds good, I wont really have time to work on this until
tomorrow evening... So hopefully you don't have a problem waiting for
another day.
I will get back to you... :)
--jld

Clarification of Question by purplepit-ga on 12 May 2003 11:29 PDT
That's fine 
Thanks
Purplepit-ga

Request for Question Clarification by jeanluis-ga on 14 May 2003 05:27 PDT
Just to let you know I haven't forgotten about you, I had a bunch of
things come up last night, but tonight after work I will have pleanty
of free time to pound this one out...
Thanks for your cooperation.
--jld
Answer  
Subject: Re: Visual c++ problem
Answered By: jeanluis-ga on 14 May 2003 16:50 PDT
Rated:5 out of 5 stars
 
Here is the solution, I have tried compiled it and run it with both
MSVC, and g++/Linux. It worked fine for me both times:

#include <string.h>
#include <stdio.h>
#define MAX_NAMES  11
#define MAX_NAME_LEN  255

class cQueue 
{
private:
    char szNameList[MAX_NAMES][MAX_NAME_LEN];
    int nListLen;
public:
    cQueue() { nListLen = 0;}
    bool add(char* szName);
    bool remove();
    void display();
};

class cPriorityQueue 
{
private:
    char szNameList[MAX_NAMES][MAX_NAME_LEN];
    int nPriorityList[MAX_NAMES];
    int nListLen;
    void insert(char* szName, int nPriority, int nIndex);
public:
    cPriorityQueue() 
    { 
        nListLen = 0; 
        memset(szNameList, 0, MAX_NAMES * MAX_NAME_LEN *
sizeof(char));
        memset(nPriorityList, 0, MAX_NAMES * sizeof(int));
    }
    bool add(char* szName, int nPriority);
    bool remove();
    void display();
};

bool cQueue::add(char* szName)
{
    if (nListLen < MAX_NAMES-1) 
    {
        strcpy(szNameList[nListLen++], szName);
    }
    else 
    {
        fprintf(stderr, "Error: Queue full\n");
        return false;
    }
    return true;
}

bool cQueue::remove()
{
    int i;
    if (nListLen > 0) {
        for (i = 0; i < nListLen; i++) 
        {
            strcpy(szNameList[i], szNameList[i+1]);
        }
        nListLen--;
    }
    else 
    {
        fprintf(stderr, "Error: Queue empty\n");
        return false;
    }
    return true;
}

void cQueue::display()
{
    int i;
    for (i = 0; i < nListLen; i++) 
    {
        printf("%s\n", szNameList[i]);
    }
}

bool cPriorityQueue::add(char* szName, int nPriority)
{
    int i;
    if ((nPriority > 10) || (nPriority < 1))
    {
        fprintf(stderr, "Error: Invalid priority\n");
        return false;
    }
    else if (nListLen < MAX_NAMES-1)
    {
        nListLen++;
        for (i = nListLen-1; i >= 0; i--) 
        {
            if ((i == 0) || (nPriorityList[i - 1] <= nPriority)) {
                insert(szName, nPriority, i);
                break;
            }
        }
    }
    else 
    {
        fprintf(stderr, "Error: Queue full\n");
        return false;
    }
    return true;
}

bool cPriorityQueue::remove()
{

    int i;
    if (nListLen > 0) {
        for (i = 0; i < nListLen; i++) 
        {
            strcpy(szNameList[i], szNameList[i+1]);
        }
        nListLen--;
    }
    else 
    {
        fprintf(stderr, "Error: Queue empty\n");
        return false;
    }
    return true;
}

void cPriorityQueue::display()
{
    int i;
    for (i = 0; i < nListLen; i++) 
    {
        printf("%s: %i\n", szNameList[i], nPriorityList[i]);
    }
    
}

void cPriorityQueue::insert(char* szName, int nPriority, int nIndex)
{
    int i;
    for (i = nListLen; i > nIndex; i--) 
    {
        strcpy(szNameList[i], szNameList[i-1]);
        nPriorityList[i] = nPriorityList[i-1];
    }
    strcpy(szNameList[nIndex], szName);
    nPriorityList[nIndex] = nPriority;
}

int main() {
    cQueue Queue;
    cPriorityQueue PriorityQueue;
    int i, nPriority;
    char szName[MAX_NAME_LEN];
    for (i = 0; i < MAX_NAMES-1; i++) 
    {
        printf("Please enter a name: ");
        fscanf(stdin, "%s", szName);
        printf("Please enter a priority: ");
        fscanf(stdin, "%i", &nPriority);
        if (PriorityQueue.add(szName, nPriority)) 
        {
            Queue.add(szName);
        }
        else 
        {
            i--;
        }
    }
    printf("\n\nRegular queue:\n");
    Queue.display();
    printf("\n\nPriority queue:\n");
    PriorityQueue.display();
    
    
    return 0;
}

Please let me know if you have any questions, or comments. I will be
more than happy to assist further.
Thanks,
--jld
purplepit-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Excellent thank you.....

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy