The questions are as follows: 1. Write a 'c' program to read the age of 100 persons and count the number of persons in the age group 50 to 60.use for loop and continue statements.(10 marks)2. Write a program to read a positive integer and print its binary equivalent.(10 marks)3. Given two one dimensional arrays a and b which are sorted in ascending order. write a program to merge them into a single sorted array ,c that contains every item from arrays a and b, in ascending order.(10 marks)4. Write a function in c that would traverse a linear singly linked list in reverse and write out the contents in reverse order.(10 marks)5. Write a program to read a set of integers and to separate all odd and even numbers. write all even numbers in ascending order.(10 marks)6. Write a program to read a real number x and find the even number nearest to x.(10 marks)7. Write a program to multiply two matrices of order m*n and n*p respectively.(10 marks)8. Write a program in'c' to implement k-map simplification technique.(10 marks)

Showing Answers 1 - 20 of 20 Answers

for 3 rd question

let us take source arrays A & B and destinaton is C

take three index variables I,J,K respectively for each array

take n and m are the size of arrays

while(i<=n &&j<=m)

{

if (a[i]<b[j])    c[k++]=a[i++]

else c[k++]=b[j++]

}

while(i<=n)  c[k++]=a[i++];

while(j<=m) c[k++]=b[j++];

then c array contains sorted data of A and B

  Was this answer useful?  Yes

I am having a recursive solution to the 2nd problem, though, one can find a non-recursive solution thru the implementation of stack.

 void  printBinary(int n)
 {
 if (n>0)
 {
  printBinary(n/2);
  printf("%c","01"[n%2]);
 }
 else
 {
  printf("\n");
 }
 }

  Was this answer useful?  Yes

Solution of q4 could also be made with recursion.

void traverseInReverse(Node *hp)
{
 if (hp!=NULL)
 {
  traverseInReverse(hp->next);
  printf("\n%d",hp->data);
 } 
 else
 {
  return;
 }
}

Three cheers for recursion. (sorry for those who always wants efficiency rather than some fun coding)

  Was this answer useful?  Yes

swetha76

  • Sep 28th, 2006
 

The answer to the first question

#include<stdio.h>
main()
{
  int age[5],i=0,count=0;
  for(i=0;i<5;i++)
  {
     printf("Enter the age of the %d person ",i+1);
     scanf("%d",&age[i]);
  }
  for(i=0;i<5;i++)
  {
    if((age[i]>=50)&&(age[i]<=60))
      count++;
   }
  printf(" The no of the people between the age group 50 and 60 is %d",count);
}

shiv reddy

  • Nov 13th, 2006
 

void agecheck(int a[])

{

int count=0; 

for(int i=0;i<100;i++)

{

if(a[i]>50 ||a[i]<=60)

{

}count++;

}

else continue;

}

}

venkat_ceg

  • Mar 20th, 2007
 

void main()
{
int i,a[15],n;
clrscr();
printf("Enter a no:n");
scanf("%d",&n);
for(i=0;i<15;i++,n>>=1)//Vary de bit size accordingly
a[i]=n&1;
for(i=15;i>=0;i--)
printf("%d",a[i]);
getch();
}


  Was this answer useful?  Yes

econos

  • May 7th, 2008
 

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

int main(void)
{    int x,y,ref;
    int numbits=0;

    printf("Input the desired number n");
    scanf("%d",&x);

    numbits=(sizeof(int)*8-1);    
    ref=(int)(pow(2,numbits));
   
    for(y=0;y<=numbits;y++)
    {    if((x&ref)>0)
            printf("1");
        else
            printf("0");
        x=x<<1;
    }
    printf("n");
    return(0);
}

The whole numbits shebang is just to make this code machine independent. If it does not work or if you know the sizeof(int) on your machine you can just hard code the values for numbits and ref.

The basic idea is that you are masking and shifting the number such that you know if a bit is set or not(MSB in our case) and this translates to a string of 1s and 0s.

  Was this answer useful?  Yes

Answer to Question 4.

Here I am giving the full program which should run without any warning and error in Turbo C++ 3.0

<--------------------------------------------------------------------------------->
#include<stdio.h>
#include<alloc.h>
struct node
{
   int data;
   struct node *link;
};
typedef struct node node;
void addatbeg(node **,int);
void display(node **);
void reverse(node **);
void main()
{
   node *p;
   p=NULL;

   addatbeg(&p,10);
   addatbeg(&p,129);
   addatbeg(&p,67);
   addatbeg(&p,23);
   printf("Linklist: n");
   display(&p);
   reverse(&p);  //reverse linklist
   printf("After Reverse linklist: n");
   display(&p);

}

void addatbeg(node **head, int num)
{
   node *temp;
   if(*head == NULL)
   {
      temp = (node *)malloc(sizeof(node));
      temp->data = num;
      temp->link = NULL;
      *head = temp;
   }
   else
   {
      temp = (node *)malloc(sizeof(node));
      temp->data = num;
      temp->link = *head;
      *head = temp;

   }
}
void display(node **head)
{
    node *temp = *head;
    while(temp != NULL)
    {
      printf("t %d",temp->data);
      temp = temp->link;
    }
}

void reverse(node **head)
{
    node *r, *s, *temp;
    r = NULL;
    temp = *head;

    while(temp != NULL)
    {
        s = r;
        r = temp;
        temp = temp->link;
        r->link = s;

    }
     *head = r;
}

<----------------------------------------------------------------------------->

The logic behind the program is:
1) First create a linklist wid some data(with the help of  addatbeg function)
2) Reverse the linklist(with the help of reverse function)
3) Display the linklist(with the help of display function)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions