Friday, 8 November 2013

Download Thapar Lab Assignment - 2 (2013) SOLVED UTA003.zip
File size: 12.5KB


Note: All the programs are tested on a GCC compiler.

Find all C Programs HERE

http://www.mavenscientists.com/2013/09/c-tutorial.html

If you like this, you can join for the free seminar here

http://www.mavenscientists.com/2013/11/request-free-seminar-faq.html

1
#include <stdio.h>

#define SIZE 100

// change the value of SIZE to generate more even numbers 

int main()
{
	int even[SIZE] = {0}; // initializing every element of array with a value of zero
	int I = 2;
	int index = 0;
	
	while(1) // always true condition
	{
		if(I % 2 == 0)
		{
			even[index] = I;
			index++;
		}
		I++;
		if(index == SIZE) // this condition is true when first 100 even numbers have been found
		{
			break;
		}
	}
	// displaying the elements of array 'even'
	for(I = 0; I<SIZE; I++)
	{
		printf("%d : %d\n", I+1, even[I]);
	}
	
	return 0;
}

2
#include <stdio.h>

#define SIZE 10

int main()
{
	//declaring and initializing an array with 0's
	int array[SIZE] = {0};
	int max;
	int I;
	
	printf("Enter 10 integer values\n\n");
	for(I = 0; I<SIZE; I++)
	{
		printf("Integer %d : ", I+1);
		scanf("%d", &array[I]);
	}
	
	max = array[0];
	for(I = 1; I<SIZE; I++)
	{
		if(max < array[I])
		{
			max = array[I];
		}
	}
	
	printf("\nMAX Element : %d", max);
	
	return 0;
}

3
#include <stdio.h>

#define SIZE 10

int main()
{
	//declaring and initializing an array with 0's
	int array[SIZE] = {0};
	int backup[SIZE] = {0};
	int I;
	int J;
	int count = 0;

	printf("Enter %d integer values\n\n", SIZE);
	for(I = 0; I<SIZE; I++)
	{
		printf("Integer %d : ", I+1);
		scanf("%d", &array[I]);
	}
	
	for(I = 0; I < SIZE; I++)
	{
		for(J = 0; J < count; J++)
		{
			if(array[I] == backup[J])
				break;
		}
		if(J == count)
		{
			backup[count] = array[I];
			count++;
		}
	}
	
	printf("Array after deleting duplicate elements\n");
	for(I = 0; I < count; I++)
	{
		printf("%d\n", backup[I]);
	}

   return 0;
}
4
/* NOTE - This program uses 'bubble sort' algorithm to sort the array
To check more methods/ algorithms to sort, go to - 
*/
#include <stdio.h>

#define SIZE 10

int main()
{
	int array[SIZE];
	int I;
	int J;
	int temp;

	printf("Enter %d integer values\n\n", SIZE);
	for(I = 0; I<SIZE; I++)
	{
		printf("Integer %d : ", I+1);
		scanf("%d", &array[I]);
	}

	for(I = 0 ; I < ( SIZE - 1 ); I++)
	{
		for(J = 0 ; J < SIZE - I - 1; J++)
		{
			if(array[J] > array[J+1]) /* For decreasing order use < */
			{
				temp = array[J];
				array[J] = array[J + 1];
				array[J + 1] = temp;
			}
		}
	}
 
	printf("\n\nArray in Ascending Order : \n");
	for(I = 0; I < SIZE ;I++)
		printf("%d\n", array[I]);
  
	return 0;
}
5
#include <stdio.h>

int main()
{
	int matrix[3][3];
	int row;
	int col;
	int rowSum;
	
	printf("Enter Matrix Values : \n");
	for(row = 0; row < 3; row++)
	{
		for(col = 0; col < 3; col++)
		{
			printf("Matrix[%d][%d] : ", row, col);
			scanf("%d", &matrix[row][col]);
		}
	}
	
	// displaying sum of each row
	for(row = 0; row<3; row++)
	{
		rowSum = 0;
		for(col = 0; col<3; col++)
		{
			rowSum += matrix[row][col];
		}
		printf("\nRow %d sum = %d", row + 1, rowSum);
	}
	
	return 0;
}

6
#include <stdio.h>

int main()
{
	int row;
	int col;
	int rowCount;
	int colCount;
	int matrix[10][10];
	int	transpose[10][10];
 
	printf("Enter number of rows in Matrix : ");
	scanf("%d", &rowCount);
	printf("Enter number of columns in Matrix : ");
	scanf("%d", &colCount);

    printf("Enter Matrix Elements :  \n");
	for(row = 0; row < rowCount; row++)
	{
		for(col = 0; col < colCount; col++)
		{
			printf("Matrix[%d][%d] : ",row, col);
			scanf("%d", &matrix[row][col]);
		}
		printf("\n");
	}

	//Original Matrix Elements
	printf("\nOriginal Matrix\n");
	for(row = 0; row < rowCount; row++)
	{
		for(col = 0; col < colCount; col++)
		{
			printf("%d   ", matrix[row][col]);
		}
		printf("\n");
	}

	
	// finding transpose
	for(row = 0; row < rowCount; row++ )
	{
		for(col = 0; col < colCount; col++ )
		{
			transpose[col][row] = matrix[row][col];
		}
	}

	// displaying transpose matrix
	printf("\nTranspose Matrix\n");
	for(row = 0; row < colCount; row++ )
	{
		for(col = 0; col < rowCount; col++ )
		{
			printf("%d\t", transpose[row][col]);
		}
		printf("\n");
	}
	
	return 0;
}
7
#include <stdio.h>
#include <string.h>

int main()
{
	char str[50] = {'\0'};
	int length = 0;
	
	printf("Enter a String : ");
	gets(str);
	
	length = strlen(str);
	printf("\nLength of String :  %d", length); 

	return 0;
}
8
#include <stdio.h>
#include <string.h>

int main()
{
	char str[50] = {'\0'};
	int length = 0;
	int I;
	
	printf("Enter a String : ");
	gets(str);
	length = strlen(str);
	for(I = 0; I< length; I++)
	{
		if(str[I] >=65 && str[I] <= 91)
		{
			str[I] += 32;
		}
	}
	
	printf("\nLowecase String : ");
	puts(str);

	return 0;
}
9
#include <stdio.h>
#include <string.h>

#defiine SIZE 100

int main()
{
	char string[SIZE];
	int i = 0;
	int start;
	int len;
	int length;
	
	printf("String : ");
	gets(string);
	printf("Start Position : ");
	scanf("%d", &start);
	printf("Length of Substring : ");
	scanf("%d", &len);
	
	length = len + start;

	if(length < strlen(string))
	{
		for(i = start; i< len+start; i++)
		{
			printf("%c", string[i]);
		}
	}
	else
	{
		printf("\nError:\nInvalid Input!"); 
		/* 
		when the length of substring is input greater than the length of string
		or
		when the start position is not a valid index in the character array
		*/
	}

	return 0;
}
10
#include <stdio.h>
#include <string.h>

#define SIZE 100

int main()
{
	char str1[SIZE] = {'\0'};
	char str2[SIZE] = {'\0'};
	int I = 0;
	int length1;

	printf("Enter a String : ");
	gets(str1);

	length1 = strlen(str1);
	
	//copying 1 string to another
	for(I = 0; I<length1; I++)
	{
		str2[I] = str1[I];
	}
	
	printf("\nString 1 : ");
	puts(str1);
	printf("\nString 2 : ");
	puts(str2);
	
	return 0;
}

11
#include <stdio.h>
#include <string.h>

#define SIZE 100

int main()
{
	char str[SIZE] = {'\0'};
	char reverse[SIZE] = {'\0'};
	int I = 0;
	int length;

	printf("Enter a String : ");
	gets(str);
	length = strlen(str);
	for(I = length; I>=0; I--)
	{
		reverse[length-I] = str[I];
	}
	
	printf("\nReverse String : ");
	for(I = 0; I<=length; I++)
	{
		printf("%c", reverse[I]);
	}
	
	return 0;
}

12
#include <stdio.h>
#include <string.h>

#define CITIES 10

int main()
{
	char a[CITIES][20]; 
	int I;
	int J;
	
	printf("Enter Names of %d cities\n", CITIES);
	for(I = 0; I<CITIES; I++)
	{
		printf("City %d : ", I+1);
		gets(a[I]);
	}

	printf("\nIn Alphabetical Order \n");
	for (I = 0; I < CITIES; I++)
	{
		for ( J = I+1; J < CITIES; J++)
		{
			if (strcmp(a[I], a[J]) > 0)
			{
				char temp[20] = {'\0'};
				strcpy(temp, a[I]);
				strcpy(a[I], a[J]); 
				strcpy(a[J], temp); 
			}
		}
		printf("City %d : %s\n", I+1, a[I]);
	}
		
	return 0;  
}
13
#include <stdio.h>

int multiply(int a, int b)
{
	return a*b;
}

int main()
{
	int num1;
	int num2;
	
	printf("Number 1 : ");
	scanf("%d", &num1);
	printf("Number 2 : ");
	scanf("%d", &num2);
	
	printf("Multiplying num1 and num2: %d",multiply(num1, num2));
	
	return 0;
	
}
14
#include <stdio.h>

long power(int base, int exponent)
{
	long result = 1;
	int I;
	for(I =0; I<exponent; I++)
	{
		result *= base;
	}

	return result;
}

int main()
{
	int base;
	int exponent;
	
	printf("Base     : ");
	scanf("%d", &base);
	printf("Exponent : ");
	scanf("%d", &exponent);
	
	printf("Result   : %ld", power(base, exponent));
	
	return 0;
	
}
15 - Call by Value
#include <stdio.h>

void swap(int num1, int num2) // formal parameters - The value of formal parameters change in call by value
{
	int temp;
	temp = num1;
	num1 = num2;
	num2 = temp;
	printf("\nAfter Swap");
	printf("\nNum1 : %d", num1);
	printf("\nNum2 : %d", num2);
	
}

int main()
{
	int num1; 
	int num2; 
	
	printf("Enter Num1 : ");
	scanf("%d", &num1);
	printf("Enter Num2 : ");
	scanf("%d", &num2);
	
	swap(num1, num2); // call by value - the value of num1 and num2(actual parameters) remain same.
	/*
	In call by value the value of the actual parameter (the value of variables that are passed) remains same. 
	The value of formal parameters (the parameters in function definition) is changed.
	*/
	
	return 0;
}
15 - Call by reference
#include <stdio.h>

/* num1 and num2 are formal parameters. 
And num1, num2 are integer type pointers - address is passed into the pointer 
and changes are made on the value present at that address.*/
void swap(int* num1, int* num2) 
{
	int temp = *num1;
	*num1 = *num2;
	*num2 = temp;
}

int main()
{
	int num1; 
	int num2; 
	
	printf("Enter Num1 : ");
	scanf("%d", &num1);
	printf("Enter Num2 : ");
	scanf("%d", &num2);
	
	swap(&num1, &num2); /* call by reference - (actual parameters)*/
	/* In call by reference method of passing arguments to a function,
	the address of the argument(actual parameters) is copied into the formal parameter. 
	Inside the function, the address is used to access the actual argument used in the call. 
	This means that changes made to the parameter affect the passed argument. */
	
	printf("\nAfter Swap");
	printf("\nNum1 : %d", num1);
	printf("\nNum2 : %d", num2);

	return 0;
}
16
#include <stdio.h>

#define SIZE 5

int main()
{
	int array[SIZE];
	int* p;
	int sum = 0;
	int I;
	
	printf("Enter %d integers\n", SIZE);
	for(I = 0; I<SIZE; I++)
	{
		printf("Number%d : ", I+1);
		scanf("%d", &array[I]);
	}
	
	p = &array[0];
	for(I = 0; I<SIZE; I++)
	{
		sum += *p;
		*p++;
	}
	
	printf("\nSum : %d", sum);
	
	return 0;
}
17
#include <stdio.h>

#define MAX 100

int main()
{
	char str[MAX] = {'\0'};
	char* p;
	int size = 0;

	printf("Enter a String : ");
	gets(str);
	
	p = &str;
	while(*p != '\0')
	{
		size++;
		p++;
	}
	
	printf("\nSize of string : %d", size);
	
	return 0;
}
18
#include <stdio.h>

#define MAX 200

int countWords(char* sentence)
{
	char* p;
	int count = 1;
	int previous = 0;
	
	p = sentence;
	
	// ignoring leading whitespace
	if(*p == ' ')
	{
		count--;
	}
	while(*p != '\0')
	{
		if(*p == ' ' && previous!= 32)
			count++;
		previous = *p;
		p++;
	}
	// ignoring trailing white space 
	p--;
	if(*p == ' ')
		count--;

	return count;
}

int main()
{
	char sentence[MAX] = {'\0'};
	int count;
	
	printf("Enter a sentence : ");
	gets(sentence);

	count = countWords(&sentence);
	printf("\nNumber of words : %d", count);
	
	return 0;
}
19
#include <stdio.h>

#define MAX 3

// change the value of MAX to 20 as per question

typedef struct
{
	int id;
	char name[50];
	int age;
	int testMatches;
	int averageScore;

}CRICKETER;


int main()
{
	CRICKETER cricketer[MAX];
	int order[MAX] = {0};
	int I;
	int J;
	int temp; //temporary 
	int array[MAX] = {0};
	
	printf("Enter Record for %d Cricketers\n", MAX);
	for(I = 0; I<MAX; I++)
	{
		cricketer[I].id = I;
		order[I] = I;
		fflush(stdin);
		printf("Cricketer #%d - ", I+1);
		printf("\n\tName : ");
		gets(cricketer[I].name);
		fflush(stdin);
		printf("\tAge  : ");
		scanf("%d", &cricketer[I].age);
		printf("\tTest Matches Played : ");
		scanf("%d", &cricketer[I].testMatches);
		printf("\tAverage Score       : ");
		scanf("%d", &cricketer[I].averageScore);
		array[I] = cricketer[I].averageScore;
	}
	
	//Display all records in ascending order (averageScore)
	
	//sort and fill order array
	for(I = 0 ; I < (MAX - 1 ); I++)
	{
		for(J = 0 ; J < MAX - I - 1; J++)
		{
			if(array[J] > array[J+1]) /* For decreasing order use < */
			{
				temp = array[J];
				array[J] = array[J + 1];
				array[J+1] = temp;
				temp = order[J];
				order[J] = order[J+1];
				order[J+1] = temp;
			}
		}
	}
	
	//display that record of order[index] in ascending order
	printf("\n\nASCENDING ORDER (by averageScore)");
	for(J = 0; J<MAX; J++)
	{
		I = order[J];
		printf("\nCricketer #%d - ", I);
		printf("\n\tName : ");
		puts(cricketer[I].name);
		printf("\tAge  : %d", cricketer[I].age);
		printf("\n\tTest Matches Played : %d", cricketer[I].testMatches);
		printf("\n\tAverage Score	    : %d", cricketer[I].averageScore);
	}
	
	return 0;
}
20
#include <stdio.h>

#define MAX 3
#define TRUE 1
#define FALSE 0

typedef struct
{
	int rollNumber;
	char name[25];
	char department[20];
	char course[20];
	int joiningYear;

}STUDENT;


void studentDataOfRollNumber(int rollNumber, STUDENT student[MAX])
{
	int zeroRecords = TRUE;
	int I;
	for(I = 0; I<MAX ; I++)
	{
		if(student[I].rollNumber == rollNumber)
		{
			printf("\n\tName         : ");
			puts(student[I].name);
			printf("\tDepartment   : ");
			puts(student[I].department);
			printf("\tCourse       : ");
			puts(student[I].course);
			printf("\tJoining Year : %d", student[I].joiningYear);
			zeroRecords = FALSE;
			printf("\n");
		}
	}
	if(zeroRecords == TRUE)
	{
		printf("\nNO MATCH FOUND");
	}
}

void recordsOfYear(int year, STUDENT student[MAX])
{
	int I;
	int zeroRecords = TRUE;
	for(I = 0; I<MAX ; I++)
	{
		if(student[I].joiningYear == year)
		{
			puts(student[I].name);
			zeroRecords = FALSE;
		}
	}
	if(zeroRecords == TRUE)
	{
		printf("\nNO MATCH FOUND");
	}
}


int main()
{
	STUDENT student[MAX];
	int I;
	int year;	
	int rollNumber;
	
	printf("Enter Record for %d students\n\n", MAX);
	for(I = 0; I<MAX; I++)
	{
		fflush(stdin);
		printf("student #%d - \n", I+1);
		printf("\tName         : ");
		gets(student[I].name);
		fflush(stdin);
		printf("\tCourse       : ");
		gets(student[I].course);
		printf("\tDepartment   : ");
		gets(student[I].department);
		fflush(stdin);
		printf("\tRoll Number  : ");
		scanf("%d", &student[I].rollNumber);
		printf("\tJoining Year : ");
		scanf("%d", &student[I].joiningYear);
	}
	
	printf("\n\nEnter year to Get Names of students joined in that year : ");
	scanf("%d", &year);
	recordsOfYear(year, student);
	
	printf("\n\nEnter rollNumber to Get Data of student : ");
	scanf("%d", &rollNumber);
	studentDataOfRollNumber(rollNumber, student);
	
	return 0;
}

Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists