Help for my assigment

This is a discussion on Help for my assigment within the C Programming forums, part of the General Programming Boards category; hello im new here can someone help me on my assigment here are the question QUESTION 1 Write a program ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    Unhappy Help for my assigment

    hello im new here
    can someone help me on my assigment

    here are the question

    QUESTION 1

    Write a program that reverses the string contents of a stack (the top and bottom elements exchange positions, the second last element exchange positions, and so forth until the entire stack is reversed). Hint: You may use a second stack. The following diagram illustrates the exchange process.

    A I
    B H
    C G
    D become F
    E ---------------> E
    F D
    G C
    H B
    I A


    and

    NO2 question are

    Assume that you are required to enter a list of strings into a computer, rearrange them in alphabetical order, and then print out the rearranged list. The strings must be stored in a two-dimensional character array. Each string will be stored in a separate row within the array. You may use any suitable string manipulation functions. One of the library functions that can be used is strcmpi.
    strcmpi compares the strings but does not differentiates between upper and lowercase characters. The function accepts two strings as arguments and returns integer value, depending on the relative order of the two strings, as follows:

    a) A negative value is returned if the first string alphabetically precedes the second string.
    b) A value of zero is returned if the first string and the second string are identical.
    c) A positive value is returned if the second string alphabetically precedes the first string.

    If the function strcmpi (string1, string2) returns a positive value then, this indicates that string2 must be placed ahead of string1 in order to rearrange the two strings in alphabetical order.

    Sample output is given as follows:

    Enter each string on a different line:

    Type ‘END’ when finished

    String 1: Panda
    String 2: Apple
    String 3: Island
    String 4: Calendar
    String 5: Brown
    String 6: Black
    String 7: Rainbow
    String 8: News
    String 9: Ballroom
    String 10: Canada
    String 11: END

    Reordered list of strings:

    String 1: Apple
    String 2: Ballroom
    String 3: Black
    String 4: Brown
    String 5: Calendar
    String 6: Canada
    String 7: Island
    String 8: News
    String 9: Panda
    String 10: Rainbow
    String 11: END


    can help me,im so confused rite now

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    err pls removed become and ----->
    sory miss spelling

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For number 1:

    Take a stack of 5 napkins. Gently put a letter A through E on these napkins, and put them in alphabetical order, with A napkin, on the top.

    Now, moving only 1 napkin at a time, re-stack the napkins, so the order is E though A.

    How'd you do that?

    That's how your program can do it, too.

    So write down the steps you took, in detail, and in order:

    You put the A napkin off the stack of napkins, and put it onto a bare space on the table.
    You put the B napkin off the stack, and put it on top of the A napkin
    You repeated that for every napkin.

    In other words, in pseudo code:
    Code:
    for each napkin in the stack
       You moved it from the old stack, and put it on the top of a new stack
    end for
    Are you using an array for your stack, or something else?

    Post up your program for this, even if it's just a start, and when we're through with that, we'll talk about the second part of the assignment.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    hemm im already asked my lecturer it said
    need to do this with stack and pointer for question 1
    and sorting in question 2

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    here are my code
    for question 1

    Code:
    #include <stdio.h>
    
    void main()
    {
    	char text[] = "ABCDEFGHI";
    	char reverse[8];
    	int i;
    	int y = 0;
    
    	for (i=8; i>=0; i--)
    	{
    		printf("%c   ", text[y]);
    		if (i==4)
    		{
    			printf("=>");
    		}
    		else
    		{
    			printf("  ");
    		}
    		reverse[y] = text[i];
    		printf("   %c\n", reverse[y]);
    		y++;
    	}
    	printf("\nAfter string was reverse: ");
    	for (i=0; i<=8; i++)
    	{
    		printf("%c", reverse[i]);
    	}
    }
    if falsed can help me im really blur rite now

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A "stack" is meant to be dealt with just like a stack of plates. You can't just reach in and grab "any old plate you want". You have to grab *just* the plate that's on top of the stack.

    (a particularly good visual is the spring loaded plate stacks in restaurants. You can only reach the top plate, and the lower plates move up (from the spring action on them), with every plate that is taken off the top.)

    So picture your array with it's zero index being the top item on the stack. Put that item onto the top of your second stack.

    Now move all the items on your old stack, up by one index. (This isn't absolutely necessary for an array, but it would be necessary with a stack which used other data structures, and since it's referred to as a "stack", then it's not being handled like just any old array.)

    This part is like the springs on the stack of plates. Usually called a "pop".

    Same thing with your second stack. You can't just keep adding on new items to the top, without pushing the other plates down one on the stack. That's usually called a "push".

    so you push items down, on one stack, and pop them up on the other.

    You always have to be careful with programming jargon, with the word "stack". Because sometimes a list of items is called a stack, but it's handled much less strictly than a stack is. And sometimes the term "stack" is used when they really mean a "queue".

    Strictly speaking, a queue is just like a line of people waiting to buy concert tickets - first come, first to buy, or "first in, first out", or FIFO.

    A stack however, is first in, last out, or FILO. Remember those spring loaded plate holders.

    So, a list of things, can be handled as a stack or as a queue. A stack is always first in, last out (FILO), "stack of plates". A queue is always first in, first out (FIFO) "concert ticket line".

    So before you can program this, you need to know how strictly your teacher wants your stack, to be. A true stack reversal is much different than just reversing order on a list of letters.

    Which way do you need to go with this - stack or queue?

    P.S. This is not the place for "text message speak". Be explicit, and spell it out, with all details. I have no idea what "falsed" means.
    Last edited by Adak; 11-19-2009 at 11:43 AM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    can u gived me exanple how to made stack and how to revesed it

  8. #8
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by cloverdagreat View Post
    can u gived me exanple how to made stack and how to revesed it
    As an example you could set all the variables and just output them with printf() in reversed order.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In pseudo code, from stack A to stack B: with 3 elements each

    pop(A)

    push(B);
    push(B);
    push(B);

    Now bring up all the values in A, by one index
    for(i = 1; i < 3; i++)
    A[i-1] = A[i];

    Now pop(A) off and push it onto B. (This is the same as two pop(B)'s ).

    pop(A);

    Code:
    push(B);
    i = 0;
    while(i < 3 && B[i+1] == 0) {   //a more general way to push a value down onto the B stack:
      B[i+1] = B[i];
      ++i;
    }
    Then bring up the values in A, by one index, and pop the one on the top, (the last one), off the A stack.

    Push it onto the B stack, and you're done.


    If it's just being handled as a list, not a stack, then use regular loops:

    Code:
    for(i = 0; i < 3; i++)
       B[2-i] = A[i];   //2 would normally be replaced by the number of data items you had
                          //but no more than the size of the B array, minus 1.
    This is all untested, and "idea' type code. Don't just try to run it and expect no adjustments to be necessary.

    Did you ever find out if they wanted this handled as a strict stack or not? That's the first thing to do, I think. If they just want the order of the letters reversed in twin arrays, that's easy. Stacks are a bit more work.

  10. #10
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Is it not more easy to just.

    Code:
    int number1, number2;
    
    number1 = 1;
    number2 = 2;
    
    printf("%d %d\n", number2, number1);
    
    getchar();
    
    return 0;

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The assignment requires reversing the contents of one stack (or list), and suggests the use of a parallel stack or array, to do it.

    So just printing them out the values in reverse order wouldn't do.

    I like the way you think, though!

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    here are my code for question 1

    Code:
    /* This is an array implementation of a character stack. */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stack.h>
    #ifndef _STACK_H
    #define _STACK_H
    
    typedef char stackchar;
    typedef struct {
      stackchar *contents;
      int maxSize;
      int top;
    } stackT;
    
    void StackInit(stackT *stackP, int maxSize);
    void StackDestroy(stackT *stackP);
    void StackPush(stackT *stackP, stackchar );
    
    stackchar StackPop(stackT *stackP);
    
    int StackIsEmpty(stackT *stackP);
    int StackIsFull(stackT *stackP);
    
    #endif
    
    void StackInit(stackT *stackP, int maxSize)
    
     {  stackchar *newContents;
    
         newContents = (stackchar *)malloc(sizeof(stackchar) * maxSize);
     if (newContents == NULL)
    
        { fprintf( "Insufficient memory to initialize stack.\n");
    
    	exit(1);
     }
    
    	stackP->contents = newContents;
    	stackP->maxSize = maxSize;
      stackP->top = -1;
    }
    	void StackDestroy(stackT *stackP)
    
    {    free(stackP->contents);
    
    	stackP->contents = NULL;
    	stackP->maxSize = 0;
    	stackP->top = -1;
    
    	}
    
    	void StackPush(stackT *stackP, stackchar )
    
    	{  if (StackIsFull(stackP))
    
    		{    fprintf(stderr, "Can't push element on stack: stack is full.\n");
    
    	exit(1);
    
    }
    
    
    	stackP->contents[++stackP->top] = char;
    	}
    
    	stackchar StackPop(stackT *stackP)
    
    
    	{if (StackIsEmpty(stackP))
    
    		{ fprintf(stderr, "Can't pop element from stack: stack is empty.\n");
    
    
    	exit(1);
    }
    
    
    	return stackP->contents[stackP->top--];
    
    	}
    
    	int StackIsEmpty(stackT *stackP)
    
    {
    
           return stackP->top < 0;
    
           }
    
           int StackIsFull(stackT *stackP)
    
     {
    	return stackP->top >= stackP->maxSize - 1;
    
    
    }

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    if got wrong can help me to
    correct it

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You tell me, does it meet your assignments requirements?

    Personally, I don't like it because it's not your code, IMO, and that's just not the way to learn programming.

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    this is the code for Q1
    err dont laugh at me hehehe
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define MAX 10
    
    struct st_a{
    	      int topmost;
    	      int stack[MAX] ;
    	 };
    struct st_a s;
    
    struct stl{
    	      int data;
    	      struct stl*next ;
    	  }*top,*p,*q;
    
    
    void stack_array();
    void stack_llist();
    
    
    
    void main()
    {
    
     int i,ch,ch1;
     char reverse[MAX];
     char c;
     clrscr();
     printf("PROGRAM TO IMPLEMENT STACKS USING ARRAY & USING LINKED LIST");
    
     do{
    
     printf(" Select method to implement stacks.../n");
     printf("1.Array");
     printf("2.linked list");
     printf("3.Exit");
     printf("your choice:");
     scanf("%d",&ch);
    
     switch(ch)
     {
      case 1:
          stack_array();
          break;
      case 2:
          stack_llist();
          break;
      case 3:
          exit();
      default:
          printf("Invalid choice");
          break;
     }
     printf("Return to main menu?y/n:");
     scanf("%s",&c);
       } while(c=='y');
    
    getch();
    }
    
    
    void stack_array()
    {
     void push_array();
     void pop_array();
     void display_array();
    
     int i,ch2 ;
     char c1;
     printf("IMPLEMENTAION OF STACKS USING ARRAY");
    
     do
      {
       printf("*******MENU*******");
       printf("1.Push\n");
       printf("2.Pop\n");
       printf("3.Display\n");
       printf("4.Exit\n");
       printf("your choice:\n");
       scanf("%d",&ch2);
       switch(ch2)
       {
        case 1:
           push_array();
           break;
       case 2:
           pop_array();
           break;
        case 3:
           display_array();
           break;
        case 4:
           exit();
        default:
        printf("Invalid choice");
          break;
       }
     printf("Want to perform another operation?y/n:");
     scanf("%s",&c1);
      } while(c1=='y');
    
    }
    
    
    int  full()
    {
     if(s.topmost==MAX)
       return 1;
     else
       return 0;
    }
    
    int empty()
    {
      if(s.topmost==0)
        return 1;
      else
        return 0;
    }
    
    void push_array()
    {
      int full();
      char ch3;
      s.topmost=0;
      do{
          if(full()==1)
          {
    	printf("The Stack is full!!");
    	break;
          }
    
          else
          {
    	s.topmost++;
    	printf("Enter the Element to be pushed : ");
    	scanf("%d",&s.stack[s.topmost]);
    	printf(" Push another element?y/n?:");
    	flushall();
    	scanf("%s",&ch3);
          }
        } while(ch3=='y' || ch3=='Y');
    }
    
    void pop_array()
    {
      int empty();
      char ch;
      do{
         if(empty()==1)
           {
    	 printf("The Stack is Empty!!");
    	 break;
           }
         else
           {
    	 printf("%d has been POPPED!",s.stack[s.topmost]);
    	 s.stack[s.topmost--]=0;
    	 printf("Pop other element?y/n:");
    	 scanf("%s",&ch);
           }
        } while(ch=='y' || ch=='Y');
    }
    
    
    void display_array()
    {
      int i;
      if(empty()==1)
         printf("No Records!!!!!!");
      else
        {
           printf("The contents of the stack are....");
           for(i=s.topmost ;i>=1; i--)
           printf("%d",s.stack[i]);
        }
    }
    
    /* Implementation of stacks using linked list */
    
    void stack_llist()
    {
     int* push_llist(struct stl *,struct stl *);
     int* pop_llist(struct stl *,struct stl *);
     void display_llist(struct stl *);
    
     int i,ch5 ;
     char c5;
     printf("IMPLEMENTAION OF STACKS USING LINKED LIST");
    
     do
      {
       printf("*****MENU*******");
       printf("1.Push\n");
       printf("2.Pop\n");
       printf("3.Display\n");
       printf("4.Exit\n");
       printf ("your choice:");
       scanf("%d",&ch5);
       switch(ch5)
       {
        case 1:
          top=push_llist(top,p);
           break;
        case 2:
           top=pop_llist(top,q);
           break;
        case 3:
           display_llist(top);
           break;
        case 4:
           exit();
        default:
        printf("Invalid choice");
          break;
       }
     printf("Want to perform another operation?y/n:");
     scanf("%s",&c5);
     }while(c5=='y');
    
    }
    
    
    int* push_llist(struct stl *top,struct stl *p)
    {
     void display_llist(struct stl *);
    
     int num;
     char c7;
    
     do{
         printf("Enter the Element to be pushed : ");
         scanf("%d",&num);
         p=malloc(sizeof(struct stl));
         p->data=num;
         p->next=NULL;
    
         if(top==NULL)
           top=p;
    
         else
           {
    	p->next=top;
    	top=p;
           }
    	printf("Push another element?y/n?:");
    	scanf("%s",&c7);
    
       } while(c7=='y' || c7=='Y');
    return(top);
    }
    
    
    int* pop_llist(struct stl *top,struct stl *q)
    {
     void display_llist(struct stl *);
     char c8;
     do{
       if(top==NULL)
         {
          printf("List is empty!");
          break;
         }
       else
         {
          q=top;
          top=top->next;
          q->next=NULL;
          free(q);
         }
    printf("Pop other element?y/n:");
    scanf("%s",&c8);
    
       } while(c8=='y' || c8=='Y');
    return(top);
    }
    
    
    void display_llist(struct stl *top)
    {
    	if(top==NULL)
    	 printf("stack is empty!!");
    	else
    	{
    	 q=top;
    	 printf ( "contents of stack are: " ) ;
    	 while ( q!= NULL )     /* traverse the entire linked list */
    	    {
    		printf ( "%d ", q -> data ) ;
    		q = q -> next ;
    	}
    	reverse[y] = text[i];
    		printf("   %c\n", reverse[y]);
    		y++;
    	}
    	printf("\nAfter string was reverse: ");
    	for (i=0; i<=8; i++)
    	{
    		printf("%c", reverse[i]);
    	}
    
    	
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While assigment?
    By Fenix in forum C Programming
    Replies: 2
    Last Post: 08-24-2009, 08:35 AM
  2. overloading the assigment operator
    By Stonehambey in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2008, 01:06 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. pointer assigment
    By spudtheimpaler in forum C Programming
    Replies: 8
    Last Post: 03-01-2004, 06:27 PM
  5. Help on Assigment!!!!!!!!!!
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2002, 09:07 PM

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