0% found this document useful (0 votes)
7 views

CPROG_IA-3_Scheme_Solutions

The document is an internal test paper for a computer science course at the Global Academy of Technology, Bengaluru. It includes programming questions related to string manipulation, statistics calculation, employee structure, file handling, and basic calculator implementation in C. Each question specifies the required tasks, expected outputs, and marks allocation.

Uploaded by

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

CPROG_IA-3_Scheme_Solutions

The document is an internal test paper for a computer science course at the Global Academy of Technology, Bengaluru. It includes programming questions related to string manipulation, statistics calculation, employee structure, file handling, and basic calculator implementation in C. Each question specifies the required tasks, expected outputs, and marks allocation.

Uploaded by

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

CIE QUESTION PAPER SET - 1 UG

ಗ ್ಲೋಬಲ್ ಅಕಾಡ ಮಿ ಆಫ್ ಟ ಕಾಾಲಜಿ, ಬ ೆಂಗಳೂರು


Global Academy of Technology , Bengaluru
Department of CSE(AI&ML) Internal Test No: III IA

1ST
Date 0 1 2 5 Semester USN

Subject Name Subject Code


Time: 75Mins. Note: Answer all full questions. Max. Marks: 40
Q. CO’s and RBT
Questions Marks
No. level
1 List and explain any five string manipulation operations using library functions
with suitable example.

String Functions:

• strlen ()
• strcmp ()
• strcpy ()
• strncmp () L1 10
• strncpy ()
• strrev ()
• strcat ()
• strstr ()
• strncat ()

Definition and Syntax - 1 M each

OR
2 Write a program to check whether a string is palindrome or not without library
function.
Executable program – 10 M

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

char *strrev(char *str) {


int len = strlen(str);
L1 10
// Temporary char array to store the
// reversed string
char *rev = (char *)malloc
(sizeof(char) * (len + 1));

// Reversing the string


for (int i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0';
return rev;
}

void isPalindrome(char *str) {

// Reversing the string


char *rev = strrev(str);

// Check if the original and reversed


// strings are equal
if (strcmp(str, rev) == 0)
printf("\"%s\" is palindrome.\n",
str);
else
printf("\"%s\" is not palindrome.\n",
str);
}

int main() {

// Cheking for palindrome strings


isPalindrome("madam");
isPalindrome("hello");

return 0;
}

3 Develop a program using pointers to compute the Sum, Mean and Standard
deviation of all elements stored in an array of N real numbers.

Executable program – 10 M

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

// Function prototypes
void computeStatistics(float *arr, int n, float *sum, float *mean, float
*std_dev);
L2 10
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

float arr[n];
printf("Enter %d real numbers: ", n);
for (int i = 0; i < n; i++) {
scanf("%f", arr + i); // Using pointer notation
}

float sum, mean, std_dev;


computeStatistics(arr, n, &sum, &mean, &std_dev);

printf("Sum: %.2f\n", sum);


printf("Mean: %.2f\n", mean);
printf("Standard Deviation: %.2f\n", std_dev);

return 0;
}

void computeStatistics(float *arr, int n, float *sum, float *mean, float


*std_dev) {
*sum = 0;
for (int i = 0; i < n; i++) {
*sum += *(arr + i);
}

*mean = *sum / n;

float variance = 0;
for (int i = 0; i < n; i++) {
variance += pow(*(arr + i) - *mean, 2);
}

*std_dev = sqrt(variance / n);


}
OR
4 Write a C program to design simple calculator using pointers.

Executable program – 10 M

#include <stdio.h>
// Function prototypes
void add(float *a, float *b, float *result);
void subtract(float *a, float *b, float *result);
void multiply(float *a, float *b, float *result);
void divide(float *a, float *b, float *result);

int main() {
float num1, num2, result; L2 10
char operation;

printf("Enter first number: ");


scanf("%f", &num1);

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operation);

printf("Enter second number: ");


scanf("%f", &num2);

switch(operation) {
case '+':
add(&num1, &num2, &result);
break;
case '-':
subtract(&num1, &num2, &result);
break;
case '*':
multiply(&num1, &num2, &result);
break;
case '/':
if (num2 != 0)
divide(&num1, &num2, &result);
else {
printf("Error! Division by zero.\n");
return 1;
}
break;
default:
printf("Invalid operator!\n");
return 1;
}

printf("Result: %.2f\n", result);


return 0;
}

void add(float *a, float *b, float *result) {


*result = *a + *b;
}

void subtract(float *a, float *b, float *result) {


*result = *a - *b;
}

void multiply(float *a, float *b, float *result) {


*result = *a * *b;
}

void divide(float *a, float *b, float *result) {


*result = *a / *b;
}

5 Create a employee structure, read and print the information of employee 10


with following fields: EMPID, EMPNAME, SALARY.

Executable program – 10 M
L3

#include <stdio.h>
// Define structure for Employee
typedef struct {
int EMPID;
char EMPNAME[50];
float SALARY;
} Employee;

int main() {
Employee emp;

// Read employee details


printf("Enter Employee ID: ");
scanf("%d", &emp.EMPID);

printf("Enter Employee Name: ");


scanf(" %49[^"]", emp.EMPNAME); // Read string with spaces

printf("Enter Employee Salary: ");


scanf("%f", &emp.SALARY);

// Print employee details


printf("\nEmployee Details:\n");
printf("EMPID: %d\n", emp.EMPID);
printf("EMPNAME: %s\n", emp.EMPNAME);
printf("SALARY: %.2f\n", emp.SALARY);

return 0;
}
OR
6 10
Write a note on following:
a) Structure with its declaration.
b) Unions with its declaration.

Structure Union

A structure is a user-defined data A union is a user-defined data type that allows


type that groups different data storing different data types at the same memory
types into a single entity. location.

The keyword struct is used to L3


The keyword union is used to define a union
define a structure

The size is the sum of the sizes of


The size is equal to the size of the largest
all members, with padding if
member, with possible padding.
necessary.

Each member within a structure is


Memory allocated is shared by individual
allocated unique storage area of
members of union.
location.

No data overlap as members are Full data overlap as members shares the same
independent. memory.
Individual member can be accessed
Only one member can be accessed at a time.
at a time.

7 Write a program in C to count the number of words and characters in a 10


file.

Executable program – 10 M

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

int main() {
FILE *file;
char filename[100], ch;
int char_count = 0, word_count = 0, in_word = 0;

// Get the filename from the user


printf("Enter the filename: ");
scanf("%s", filename);

// Open the file in read mode


file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
L2
// Read the file character by character
while ((ch = fgetc(file)) != EOF) {
char_count++;

if (isspace(ch)) {
in_word = 0;
} else if (!in_word) {
in_word = 1;
word_count++;
}
}

// Close the file


fclose(file);

// Print the results


printf("Total characters: %d\n", char_count);
printf("Total words: %d\n", word_count);

return 0;
}

OR
Write a program in C to create and store information in a text file and 10
print the same on console.

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

int main() {
FILE *file;
char filename[100], ch;

// Get the filename from the user


printf("Enter the filename: ");
scanf("%s", filename);

// Open file in write mode


file = fopen(filename, "w");
if (file == NULL) {
printf("Could not open file %s for writing\n", filename);
return 1;
}

// Get user input and write to file


8 printf("Enter text (Press Ctrl+D to stop input):\n"); L2
getchar(); // To consume leftover newline
while ((ch = getchar()) != EOF) {
fputc(ch, file);
}
fclose(file);

// Open file in read mode


file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s for reading\n", filename);
return 1;
}

// Read and display content of file


printf("\nContent of the file:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy