0% found this document useful (0 votes)
471 views24 pages

Slips Sloution Advanced C

The document contains C programs and questions on various advanced C concepts: 1. The first section contains programs that use macros, pointers, and structures. This includes finding the maximum of two numbers using a macro, calculating array sum and average using pointers, and accepting student details using a structure. 2. The second section contains programs that handle command line arguments, file I/O, string manipulation functions. This includes reversing command line arguments, displaying file contents, and a menu-driven string operations program. 3. The third section contains programs on string length calculation using strlen, dynamic memory allocation to calculate sum and average, and nested structures to accept multiple person details. 4. The fourth section passes

Uploaded by

rishichordiya24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
471 views24 pages

Slips Sloution Advanced C

The document contains C programs and questions on various advanced C concepts: 1. The first section contains programs that use macros, pointers, and structures. This includes finding the maximum of two numbers using a macro, calculating array sum and average using pointers, and accepting student details using a structure. 2. The second section contains programs that handle command line arguments, file I/O, string manipulation functions. This includes reversing command line arguments, displaying file contents, and a menu-driven string operations program. 3. The third section contains programs on string length calculation using strlen, dynamic memory allocation to calculate sum and average, and nested structures to accept multiple person details. 4. The fourth section passes

Uploaded by

rishichordiya24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Advanced C Slips Solution

Slip 1)

1) Write a program to find maximum of two numbers using macro. (5 marks).

#include<stdio.h>

#include<stdlib.h>

#define max(a,b) ((a)>(b) ? (a) : (b) )

Int main()

int x, y, result;

printf("Enter number 1: ");

scanf("%d", &x);

printf("Enter number 2: ");

scanf("%d", &y);

result = max(x,y);

printf("Maximum of %d and %d is %d \n", x, y, result);

return 0;

2) Write a program to accept to compute sum and average of all elements in an array using pointer.
(10 marks).

#include<stdio.h>

int main()

int *ptr, sum=0, number[10],i=0;

float average;

printf("Enter how many elemnets you want to store in an array: ");

scanf("%d",&number);

for(i=0; i<number; i++)


{

printf("\n Enter %d th element ",i);

scanf("%d",&number[i]);

i=0;

ptr=&number[0];

for(i=0;i<5;i++)

sum = sum + (*ptr++);

average=sum/number;

printf("\n Sum of array elements = %d", sum);

printf("\n Average of array elements = %f", average);

return 0;

3) Write a program to accept details of n students (roll number, name, percentage) using structure.
Display details of the student having maximum percentage. (15 marks)

#include<stdio.h>

struct student

char name[25];

int roll_no;

float percentage;

};

void main()

int number;
printf("Enter number of students data you want to accept : ");

scanf("%d", &number);

struct student s[number];

int i;

for(i=0;i<number;i++)

printf("Enter number of student %d : \n", i+1);

printf("Enter Roll Number ");

scanf("%d", &s[i].roll_no);

printf("Enter Name ");

scanf("%s", &s[i].name);

printf("Enter Percentage ");

scanf("%f", &s[i].percentage);

//find the students with the maximum percentage

int max=0;

for(i=1;i<number;i++)

if(s[i].percentage > s[max].percentage)

max=i;

printf("\n Details of the student with the maximum percentage: \n");

printf(" Roll Number : %d\n", s[max].roll_no);

printf(" Name : %s\n",s[max].name);

printf("Percentage : %f\n", s[max].percentage);


return 0;

Slip 2

1) Write a program to display the command line arguments in reverse order. (5 marks).

#include <stdio.h>

int main(int argc, char *argv[]) {

if (argc < 2)

printf("No command line arguments provided.\n");

return 1;

printf("Command line arguments in reverse order:\n");

for (int i = argc - 1; i > 0; i--)

printf("%s\n", argv[i]);

return 0;

2) Write a program to display contents of file “a.txt” on the screen. (10 marks)

#include <stdio.h>

#include <ctype.h>

int main()

FILE *file;
char ch;

// Open file in read mode

file = fopen("a.txt", "r");

// Check if file exists and can be opened

if (file == NULL)

printf("Unable to open the file.\n");

return 1;

// Read and display contents character by character

printf("Contents of the file 'a.txt':\n");

while ((ch = fgetc(file)) != EOF)

printf("%c", ch);

// Close the file

fclose(file);

return 0;

3) Write a menu driven program to perform the following operations on strings using standard
library functions:

1. Convert string to upper case.

2. Copy one string to another.

3. Compare two strings.

4. Concatenate two strings.


#include <stdio.h>

#include <string.h>

int main()

char choice;

char str1[100], str2[100];

printf("Enter string 1: ");

scanf("%s",&str1);

printf("Enter string 2: ");

scanf("%s",&str2);

printf("\nMenu:\n");

printf("1. Convert string 1 to uppercase\n");

printf("2. Copy string 2 to string 1\n");

printf("3. Compare string 1 and string 2\n");

printf("4. Concatenate string 1 and string 2\n");

printf("Enter your choice: ");

scanf("%c", &choice);

switch(choice)

case '1':

for(int i = 0; str1[i] != '\0'; i++)

str1[i] = toupper(str1[i]);

printf("String 1 in uppercase: %s", str1);

break;
case '2':

strcpy(str1, str2);

printf("String 2 copied to string 1: %s", str1);

break;

case '3':

if(strcmp(str1, str2) == 0)

printf("Both strings are equal.\n");

else

printf("Strings are not equal.\n");

break;

case '4':

strcat(str1, str2);

printf("Concatenated string: %s", str1);

break;

default:

printf("Invalid choice!");

return 0;

Slip 3

1) Write a program to calculate length of string using standard library function.(5 marks)

#include<stdio.h>
#include<string.h>
int main()
{
char string[25];
printf("Enter a string : \t\t");
scanf("%s",&string);

printf("The length of entered string is : %d", strlen(string));


return 0;
}

2) Write a program to allocate memory dynamically for n integers. Accept the elements and
calculate their sum and average.(10 marks)

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

int main()
{
int n, *arr, sum = 0;
float average;

printf("Enter the number of integers: ");


scanf("%d", &n);

arr = (int*) malloc(n * sizeof(int));

if (arr == NULL)
{
printf("Memory allocation failed.\n");
return 1;
}

printf("Enter %d integers:\n", n);


for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}

average = (float)sum / n;

printf("Sum of the integers: %d\n", sum);


printf("Average of the integers: %f\n", average);

free(arr);

return 0;
}

3) Write a program to declare a structure person (name, address) which


Contains another structure birthdate (day, month, year). Accept the details
of n persons and display them. (15 marks)
#include <stdio.h>

struct birthdate
{
int day;
int month;
int year;
};

struct person
{
char name[50];
char address[100];
struct birthdate dob;
};

int main()
{
int n;

printf("Enter the number of persons: ");


scanf("%d", &n);

// Array of structures to store persons


struct person persons[n];

// Input details for each person


for (int i = 0; i < n; i++)
{
printf("\nEnter details for person %d:\n", i + 1);
printf("Name: ");
scanf("%s", persons[i].name);
printf("Address: ");
scanf("%s", persons[i].address);
printf("Enter birthdate (day month year): ");
scanf("%d %d %d", &persons[i].dob.day, &persons[i].dob.month, &persons[i].dob.year);
}

// Display details of each person


printf("\nDetails of persons:\n");
for (int i = 0; i < n; i++) {
printf("\nPerson %d:\n", i + 1);
printf("Name: %s\n", persons[i].name);
printf("Address: %s\n", persons[i].address);
printf("Birthdate: %d/%d/%d\n", persons[i].dob.day, persons[i].dob.month,
persons[i].dob.year);
}

return 0;
}

Slip 4

1) Write a program to calculate sum of two numbers. Pass the numbers as command line
arguments.

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

int main(int argc, char *argv[])


{
// Check if exactly two command-line arguments are provided
if (argc != 3)
{
printf("Usage: %s <number1> <number2> \n", argv[0]);
return 1;
}

// Convert command-line arguments to integers


int num1 = atoi(argv[1]);
int num2 = atoi(argv[2]);

int sum = num1 + num2;

printf("Sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

2) Write a program to create student structure having fields roll no, name. Accept details of
one student and write a function to display the details.

#include <stdio.h>

struct student
{
int roll_no;
char name[50];
};
void displayStudent(struct student s)
{
printf("Roll No: %d\n", s.roll_no);
printf("Name: %s\n", s.name);
}

int main()
{
struct student s;

printf("Enter Roll No: ");


scanf("%d", &s.roll_no);
printf("Enter Name: ");
scanf("%s", &s.name);

printf("\nDetails of the student:\n");


displayStudent(s);

return 0;
}

3) Write a program to copy contents of file a.txt to b.txt by changing the case of each
alphabet.

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

int main()
{
FILE *sourceFile, *destinationFile;
char ch;

// Open source file in read mode


sourceFile = fopen("a.txt", "r");

// Check if source file exists and can be opened


if (sourceFile == NULL)
{
printf("Unable to open the source file.\n");
return 1;
}

// Open destination file in write mode


destinationFile = fopen("b.txt", "w");

// Check if destination file can be opened


if (destinationFile == NULL) {
printf("Unable to create/open the destination file.\n");
fclose(sourceFile); // Close the source file
return 1;
}

// Copy contents of source file to destination file, changing case


while ((ch = fgetc(sourceFile)) != EOF) {
if (islower(ch))
ch = toupper(ch);
else if (isupper(ch))
ch = tolower(ch);

fputc(ch, destinationFile);
}

// Close the files


fclose(sourceFile);
fclose(destinationFile);

printf("Contents of file 'a.txt' copied to 'b.txt' with case changed.\n");

return 0;
}

Slip 5

1) Write a program to interchange two numbers using pointers.

#include <stdio.h>

void interchange(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int num1, num2;

printf("Enter the first number: ");


scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

printf("\nOriginal numbers: num1 = %d, num2 = %d\n", num1, num2);


interchange(&num1, &num2);

printf("Numbers after interchange: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

2) Write a program to perform the following operations on two strings using standard library
functions:

a. Copy b. Compare

#include <stdio.h>

#include <string.h>

void copy_strings(char *str1, char *str2)

strcpy(str2, str1);

int compare_strings(char *str1, char *str2)

return strcmp(str1, str2);

int main()

char str1[100], str2[100];

printf("Enter the first string: ");

scanf("%s", str1);
printf("Enter the second string: ");

scanf("%s", str2);

copy_strings(str1, str2);

int comparison_result = compare_strings(str1, str2);

printf("Copied string: %s\n", str2);

if (comparison_result == 0)

printf("Strings are equal\n");

else

printf("Strings are not equal\n");

return 0;

3) Write a program to read the contents of a text file and count the number of characters, lines
and words in the file.

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

#define MAX_FILE_NAME 100


#define MAX_WORD_LENGTH 50

int main()
{
FILE *file;
char filename[MAX_FILE_NAME];
char word[MAX_WORD_LENGTH];
int charCount = 0, wordCount = 0, lineCount = 0;
char ch;

printf("Enter the filename: ");


scanf("%s", filename);

file = fopen(filename, "r");

if (file == NULL)
{
printf("Could not open file %s", filename);
return 1;
}

while ((ch = fgetc(file)) != EOF)


{
charCount++;

if (isspace(ch))
{
wordCount++;
}

if (ch == '\n')
{
lineCount++;
}
}

wordCount++;

printf("Number of characters: %d\n", charCount);


printf("Number of words: %d\n", wordCount);
printf("Number of lines: %d\n", lineCount);

fclose(file);

return 0;
}

Slip 6

1) Write a C program to compare two strings using standard library function. (5 marks)
#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100];

printf("Enter the first string: ");

scanf("%s", str1);

printf("Enter the second string: ");

scanf("%s", str2);

if (strcmp(str1, str2) == 0) {

printf("Strings are equal.\n");

} else {

printf("Strings are not equal.\n");

return 0;

2) Write a C program to copy contents of one file to another file. (10 marks)

#include <stdio.h>

#include <stdlib.h>

int main() {

FILE *sourceFile, *destinationFile;

char sourceFileName[100], destinationFileName[100];

char ch;

// Input source file name


printf("Enter the source file name: ");

scanf("%s", sourceFileName);

// Input destination file name

printf("Enter the destination file name: ");

scanf("%s", destinationFileName);

// Open source file in read mode

sourceFile = fopen(sourceFileName, "r");

if (sourceFile == NULL) {

printf("Unable to open source file.\n");

return 1;

// Open destination file in write mode

destinationFile = fopen(destinationFileName, "w");

if (destinationFile == NULL) {

printf("Unable to create or open destination file.\n");

fclose(sourceFile);

return 1;

// Copy contents character by character

while ((ch = fgetc(sourceFile)) != EOF) {

fputc(ch, destinationFile);

printf("File copied successfully.\n");


// Close files

fclose(sourceFile);

fclose(destinationFile);

return 0;

3) Write a C program to create structure employee (id, name, salary). Accept details of n
employees and perform the following operations:
a. Display all employees.

b. Display details of all employees having salary > ____.

#include <stdio.h>

#include <stdlib.h>

struct Employee

int id;

char name[50];

float salary;

};

int main()

int n, i;

float minSalary;

printf("Enter the number of employees: ");

scanf("%d", &n);

struct Employee *employees = (struct Employee *)malloc(n * sizeof(struct Employee));

for (i = 0; i < n; i++)

printf("\n Enter details for employee %d:\n", i + 1);


printf("ID: ");

scanf("%d", &employees[i].id);

printf("Name: ");

scanf("%s", employees[i].name);

printf("Salary: ");

scanf("%f", &employees[i].salary);

printf("\nAll employees:\n");

for (i = 0; i < n; i++)

printf("ID: %d, Name: %s, Salary: %.f\n", employees[i].id, employees[i].name,


employees[i].salary);

printf("\nEnter the minimum salary for filtering: ");

scanf("%f", &minSalary);

printf("\nEmployees with salary greater than %.f : \n", minSalary);

for (i = 0; i < n; i++)

if (employees[i].salary > minSalary)

printf("ID: %d, Name: %s, Salary: %.f\n", employees[i].id, employees[i].name,


employees[i].salary);

free(employees);

return 0;

Slip 7

1) Write a C program to find maximum of two numbers using macro.


#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

int maximum = MAX(num1, num2);

printf("Maximum of %d and %d is %d.\n", num1, num2, maximum);

return 0;
}

2) Write a C program to store the squares of numbers 1 to n in “squares.txt” file.

#include <stdio.h>

int main() {

int n;

// Input the value of n

printf("Enter the value of n: ");

scanf("%d", &n);

// Open file for writing

FILE *file = fopen("squares.txt", "w");

// Check if file was opened successfully

if (file == NULL) {

printf("Error opening file!\n");

return 1;
}

for (int i = 1; i <= n; i++) {

fprintf(file, "%d^2 = %d\n", i, i * i);

fclose(file);

printf("Squares of numbers from 1 to %d have been written to squares.txt\n", n);

return 0;

3) Write a C program to accept n elements using dynamic memory allocation and calculate the
sum and average of the elements. Also find the largest element.

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

int main()
{
int n, i;
float *arr, sum = 0, average, largest;

printf("Enter the number of elements: ");


scanf("%d", &n);

// Dynamically allocate memory for the array


arr = (float *)malloc(n * sizeof(float));

// Check if memory allocation was successful


if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

// Input elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &arr[i]);
}

// Calculate sum and find largest element


largest = arr[0];
for (i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] > largest) {
largest = arr[i];
}
}

// Calculate average
average = sum / n;

// Output sum, average, and largest element


printf("Sum: %f\n", sum);
printf("Average: %f\n", average);
printf("Largest element: %f\n", largest);

free(arr);

return 0;
}

Slip 8

1) Write a C program for swapping two numbers using pointers.

#include <stdio.h>

void swap(int *num1, int *num2)


{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}

int main()
{
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

swap(&num1, &num2);

printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);

return 0;
}

2) Write a C program to count the number of vowels and consonants in a string.

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

int main() {
char str[100];
int vowels = 0, consonants = 0, i;

// Input string from user


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

// Iterate through each character of the string


for (i = 0; i < strlen(str); i++)
{
char ch = tolower(str[i]);

if ((ch >= 'a' && ch <= 'z'))


{

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


{
vowels++;
} else
{
consonants++;
}
}
}

printf("Number of vowels: %d\n", vowels);


printf("Number of consonants: %d\n", consonants);

return 0;
}
3) Write a C program to accept details of n items (code, name, price) using structure.
Perform the following operations:
a. Display all items having price > ___

b. Search for an item by name.

You might also like