Acpp Cat 2 Set 1 Answers
Acpp Cat 2 Set 1 Answers
What will be the values for argc and argv[] when the input “ run with my
values” is passed as command line arguments?
When "run with my values" is passed as command-line arguments, argc will
be 2 and argv[] will contain the strings "run", and "with my values".
2. Write about different error handling functions on files.
Function Purpose
fopen() Opens a file, returns NULL on error
perror() Prints error message based on errno
strerror() Returns string describing errno
feof() Checks for end-of-file
ferror() Checks for file operation errors
clearerr() Clears file error and EOF flags
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char name[50];
int marks, n;
// Write to file
fprintf(fp, "Name: %s\tMarks: %d\n", name, marks);
}
fclose(fp);
printf("\nStudent data written to students.txt successfully.\n");
return 0;
}
Output:
Student 1
Enter name: Alice Johnson
Enter marks: 85
Student 2
Enter name: Bob Smith
Enter marks: 90
5. Outline the logic to swap the contents of two identifiers without using third
variable.
To swap the contents of two identifiers without using a third variable, we can
employ various methods like addition and subtraction, multiplication and
division, or the XOR operation.
// Let a = 5 and b = 10
// Step 2: b = a - b; (b becomes 5)
b = a - b;
// Step 3: a = a - b; (a becomes 10)
a = a - b;
Concatenation
Repetition
Indexing
Slicing
Splitting
Joining
PART-B
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
struct Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.roll_no);
printf("Enter Name: ");
scanf(" %[^\n]", s.name); // read with spaces
printf("Enter Marks: ");
scanf("%f", &s.marks);
struct Student s;
printf("\n--- All Student Records ---\n");
while (fread(&s, sizeof(struct Student), 1, fp)) {
printf("Roll No: %d | Name: %s | Marks: %.2f\n", s.roll_no, s.name,
s.marks);
}
fclose(fp);
}
struct Students;
if (!found) {
printf("Student with Roll No %d not found.\n", target_roll);
}
fclose(fp);
}
// Menu-driven main function
int main() {
int choice;
do {
printf("\n--- Student Record ---\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Search by Roll Number\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: addStudent(); break;
case 2: displayStudents(); break;
case 3: searchByRollNo(); break;
case 0: printf("Exiting...\n"); break;
default: printf("Invalid choice! Try again.\n");
}
} while (choice != 0);
return 0;
}
Student Record
1. Add Student
2. Display All Students
3. Search by Roll Number
0. Exit
Enter choice: 1
Enter Roll Number: 101
Enter Name: Alice Brown
Enter Marks: 89.5
Student added successfully!
# Display result
print(f"Letters: {letters}")
print(f"Digits: {digits}")
Output:
Write a program to check if the word ‘open’ is present in the “This is open
source software”.
Output:
List the three of conditional statements and explain them. Write a python program to
accept two numbers find the greatest and print the result.
Write a python program to accept two numbers find the greatest and print the
result.
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output:
b is not greater than a
Residential (R)
First 100 units:Rs 1.5/unit
Next 200 units:Rs2.5/unit
Above 300 units:Rs3.5/unit
Commercial(c)
Flat Rs5/unit for all consumption
Industrial(I)
First 500 units:Rs 4/unit
Above 500 units:Rs 6/unit
PROGRAM:
#include <stdio.h>
int main() {
char type;
int units;
float bill = 0;
#ifdef COMMERCIAL
if (type == 'C') {
bill = units * 5.0;
}
#endif
#ifdef INDUSTRIAL
if (type == 'I') {
if (units <= 500)
bill = units * 4.0;
else
bill = 500 * 4.0 + (units - 500) * 6.0;
}
#endif
// Display the bill
if (bill > 0)
printf("Total bill for %c customer: Rs %.2f\n", type, bill);
else
printf("Invalid customer type or data.\n");
return 0;
}
Output: