Sunday, 5 November 2017

CO4 Assignment question 14th question



Given stack have positive and negative integers, denoting different types of parentheses. The positive numbers xi denotes opening parentheses of type +x and negative number –x denotes closing parentheses of type xi. Open parentheses must be closed by the same type of parentheses. Open parentheses must be closed in the correct order, i.e., never close an open pair before its inner pair is closed (if it has an inner pair). Thus, [1,2,−2,−1]is balanced, while [1,2,−1,−2] is not balanced. You have to find out the length of the longest sub array that is balanced. Input: 5 1 -1 2 3 -2 Output: 2 Explanation The longest sub array that is balanced is (1,−1) (2,3,−2) is not balanced as (3) is not balanced.

Solution:

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
struct stack
{
               int data[MAX],top;
}st;
void push(int t)
{
               if(st.top == MAX-1)
               {
                              printf("cannot push --stack full");
                              return;
               }
               else
               {
                              st.top++;
                              st.data[st.top]=t;
               }
}

int pop()
{
               int del;
               if(st.top==-1)
               {
                              //printf("cannot delete as stack empty\n");
                              return;
               }
               del=st.data[st.top];
               st.top--;
               return del;
}
main()
{
               int n,p,c=0,i;
               st.top=-1;
               printf("enter a number enter 999 to exit");
               while(n != 999 )
               {
                              scanf("%d",&n);
                              if(n>0)
                                              push(n);
                              else if(n<0)
                              {
                                             p=pop();
                                             if(p+n == 0)
                                                            c=c+2;
                                             else
                                             {
                                                            while(st.top>-1)
                                                                           st.top--;
                                             }             
                              }
               }
               printf("length of longest sub array=%d",c);
}

Output:

                             

Friday, 3 November 2017

CO4 Assignment 6th Question solution

Sunny is fond of Vowels. In a given line of text he wanted to see all lower-case vowels in uppercase & vice-versa. Write a C program to implement Sunny’s wish.
Ex:
Input: India is my country.
Output: IndIA Is my cOUntry.

Solution:



#include <stdio.h>
main()
{
               char str[100];
               int i;
               printf("enter a string");
               gets(str);
               for(i=0;str[i]!='\0';i++)
               {
                              if(str[i] == 'A'|| str[i] == 'E'||str[i] == 'I'||str[i] == 'O'||str[i] == 'U')
                                             str[i]=str[i]+32;
                              if(str[i] == 'a'|| str[i] == 'e'||str[i] == 'i'||str[i] == 'o'||str[i] == 'u')
                                             str[i]=str[i]-32;
               }
               puts(str);
}

CO4 Assignment 5th Question solution


Define a structure named census with the following three members:
A character array city[] to store names
A long integer to store population of the city
A float member to store the literacy level.
Write a program to read details of 5 cities randomly and sort the list based on literacy level.

#include <stdio.h>
struct census
{
          char city[50];
          long int population;
          float li;
};

main()
{
          struct census temp,c[5];
          int i,p,j;
          for(i=0;i<5;i++)
          {
                    printf("\nenter city name, population and literacy level");
                    scanf("%s%ld%f",&c[i].city,&c[i].population,&c[i].li);    
          }
          for(p=0;p<4;p++)
          {
                    for(j=0;j<5-p-1;j++)
                    {
                              if(c[j].li >c[j+1].li)
                              {
                                        temp=c[j];
                                        c[j]=c[j+1];
                                        c[j+1]=temp;
                              }
                    }
          }
          printf("\nsorted order are\n");
          for(i=0;i<5;i++)
          {
                    printf("%s\t%ld\t%f\n",c[i].city,c[i].population,c[i].li);     
          }
}