Showing posts with label Module 1. Show all posts
Showing posts with label Module 1. Show all posts

Wednesday, 7 September 2016

Sum and Average of array elements using pointer

Write a C program to read 10 integers and store them in an array using pointers. Print their sum and average


#include<stdio.h>
#include<math.h>
void main()
{
                int a[10],sum=0;
                float avg=0;
                int i;
               
                printf("\nEnter the n array elements:");
                for(i=0;i<10;i++)
                                scanf("%d",(a+i));

                for(i=0;i<10;i++)
                                sum=sum+*(a+i);
   
                avg = (float)sum/10;

                printf("\nSum is = %d", sum);
                printf("\nMean is = %f", avg);
}

Output:
Enter the n array elements:1
2
3
4
5
6
7
8
9
10

Sum is = 55

Mean is = 5.500000

String Palindrome without Library function

Write a function that accepts a string and returns 1 if the string is palindrome else 0 if string is not a palindrome without using any built in functions.

#include<stdio.h>
int palindrome(char str[10])
{
                int i, j;
                i=0;
                while(str[i]!='\0')
                {
                                i++;
                 }
                for(j=i-1, i=0;   j>=0;    j--,i++)
                {
                                if(str[i] != str[j])
                                return 0;
                }
                return 1;
}
void main()
{
                char str[10];
                int p;
                printf("\nEnter a string:   ");
                gets(str);

                p=palindrome(str);
                if(p)
                                printf("\nString is palindrome");
                else
                                printf("\nString is not palindrome");
}
Output:
Enter a string:   abcde
String is not palindrome

Enter a string:   abcba
String is palindrome


Method 2:
#include <stdio.h>
int main()
{
                char string[20];
                int i, length;
                int flag = 0;
                printf("Enter a string:");
                scanf("%s", string);

                length=0;
                while(string[length]!='\0')
                                length++;

                for(i=0;i < length ;i++)
                {
                                if(string[i] != string[length - i - 1])
                                {
                                                flag = 1;
                                                break;
                                }
                }
                if (flag)
                                  printf("\nstring is not a palindrome");
                else
                                  printf("\nstring is a palindrome");
  

}

Friday, 2 September 2016

Structures and unions question set

Structures and unions

Data Structures 15CS33


1.      Write a C program to represent a complex number using structure and add two complex numbers.
(December 2007)
(8 marks)

2.      Write short notes on: structures, Nested structures and union along with examples.
(December 2007)
(December 2009)
(June 2012)
(December 2014)
(8 marks)

3.   What is structure? how it is different from an array? Explain different types of structure declaration with example and its initialization.
(June 2009)
(December 2012)
(June 2015)
(8 marks)

4.      Write a appropriate structure definition and variable declarations to store following information about 100 students.
Name, USN, Gender, Date Of Birth, and marks in 3 subjects S1, S2, S3.
Date of birth should be a structure containing fields day, month, and year.
      (June 2010)
(5 marks)

5.      Write a C program to represent a complex number using structure and multiply two complex numbers.
(December 2010)
(8 marks)

6.      How structures can be passed to function through pointers. Explain with examples.
(June 2011)
(5 marks)

7.      Develop a structure to represent a planets in the solar system. Each planet has the field for the planets name, its distance from the sun in miles and the number of moon it has. Write a program to read the data for each planet and store. Also print the name of the planet that has the highest number of moons.
(December 2011)
(December 2013)
(8 marks)

8.      Write a C program with an appropriate structure definition and variable declaration to store information about an employee using nested structure. Consider the following fields like Ename, Empid, DOJ(Date, month, year) and Salary ( Basic, DA, HRA).
(June 2012)
(December 2014)
(12 marks)

9.      Give 3 different ways of defining structure and declaring variables and method of accessing members of structures using a student structure with roll number, name, and marks in 3 subjects as members of that structure as example.
(June 2013)
(6 marks)

10.  Define a structure for the employee with the following fields:
     Emp_id(integer) , emp_name(string), emp_basic(float), emp_dest(string) and emp_age(integer).          Write the following function to process the employee data:
1)      Function to read the employee data
2)      Function to print the employee record.
      (June 2014)
      (8 marks)


Arrays Questions set

Array
Data Structures 15CS33


1.      For a given sparse matrix and its transpose, give the triplet represntation using one dimensional array, a is the given sparse matrix, b will be its transpose.

             

(December 2011)
(Dec 2013)
(December 2015)
(June 2015)
(8 marks)

2.      Consider two polynomials A(x) = 2x100 +  1 and B(x) = x4 + 10x3 + 3x2 + 1, show diagrammatically , how these two polynomials can be stored in single 1D array. Also give its C representation.
(December 2011)
(June 2015)
(12 marks)

3.      Give ADT Sparse matrix and show with a suitable example sparse matrix representation storing as triples. Give search function to search for a key in triples and Give simple transpose function to transpose sparse matrix.
(June 2013)
(8 marks)

4.      How would you represent two sparse polynomials using array of structures and also write a function to add that polynomial and store the result in the same array.
(June 2013)
(December 2015)
(6 marks)

5.      What is polynomial? What is the degree of the polynomial? Write a function to add two polynomails.
(December 2013 )
(June 2015)
(8 marks)

6.      Explain with example, dynamic memory allocation for 2 D arrays.
(June 2014)
(4 marks)

7.      Write the fast transpose algorithm for sparse matrix. Why the name sparse matrix?
(June 2014)
(8 marks)

8.      Write a note on dynamically allocated arrays using example.
(December 2015)

(6 marks)

Strings Questions set

Strings

Data Structures 15CS33 


1.      Implement i) Copying one string to another ii) Reversing the given string. Without using string library functions in 'C'.        
(December 2007)
(12 marks)

2.      Write a function that accepts a string and returns 1 is the string is palindrome else 0 if string is not a palindrome without using any built in functions.
(June 2009)
(6 marks)

3.      Explain with syntax following string handling functions.
       a)      strncpy b) strcat c)strcmp
(December 2009)
(6 marks)

4.      What is string? How string is declared and initialized?
(June 2010)
(05 marks)


DMA questions set

Dynamic Memory Allocation Function
Data Strutures 15CS33

1.      Explain what is static and dynamic memory allocation? Explain with examples dynamic memory allocation functions.
(December 2007)
(June 2009)
(December 2010)
(June 2012)
(December 2012)
(December 2014)
(December 2015)
(9 marks)


Thursday, 18 August 2016

Conversion of Infix expression to Postfix expression Example 2

Conversion of Infix expression to Postfix expression

Example 2:

Given Infix Expression:         7 + 5 * 3 ^ 2 / ( 9 – 2 ^ 2 ) + 6 * 4



Symbol
Operator Stack
Postfix String
[0]
[1]
[2]
[3]
[4]

1
7






7
2
+
+





7
3
5
+





75
4
*
+
*




75
5
3
+
*




753
6
^
+
*
^



753
7
2
+
*
^



7532
8
/
+
/




7532^*
9
(
+
/
(



7532^*
10
9
+
/
(



7532^*9
11
-
+
/
(
-


7532^*9
12
2
+
/
(
-


7532^*92
13
^
+
/
(
-
^

7532^*92
14
2
+
/
(
-
^

7532^*922
15
)
+
/




7532^*922^-
16
+
+





7532^*922^-/+
17
6
+





7532^*922^-/+6
18
*
+
*




7532^*922^-/+6
19
4
+
*




7532^*922^-/+64
20

Stack empty
7532^*922^-/+64*+


So the postfix expression is  7532^*922^-/+64*+