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

Assignment#05: #Include # Define A B A B A B A B A B A B A B A B

The document contains 5 programming questions involving pointers and arrays in C. Question 1 defines functions for basic math operations and uses function pointers to call the appropriate operation based on user input. Question 2 calculates the factorial of a number by passing a pointer to a recursive function. Question 3 counts the vowels in a string by passing a character pointer to a function. Question 4 searches a 2D array of names for a target name. Question 5 stores names and phone numbers in a 2D array and searches for a number.

Uploaded by

Abdullah Abid
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)
39 views

Assignment#05: #Include # Define A B A B A B A B A B A B A B A B

The document contains 5 programming questions involving pointers and arrays in C. Question 1 defines functions for basic math operations and uses function pointers to call the appropriate operation based on user input. Question 2 calculates the factorial of a number by passing a pointer to a recursive function. Question 3 counts the vowels in a string by passing a character pointer to a function. Question 4 searches a 2D array of names for a target name. Question 5 stores names and phone numbers in a 2D array and searches for a number.

Uploaded by

Abdullah Abid
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/ 7

Assignment#05

Q#01
#include <stdio.h>
# define ops 4
float add(float a, float b) {
return a + b;
}
float sub(float a, float b) {
return a - b;
}
float mul(float a, float b) {
return a * b;
}
float div(float a, float b) {
return a / b;
}
int main() {
printf("/*=================================================================\n");
printf("/* CSEE1122: Computer Programming\n");
printf("/* Student Name : Abdullah Abid\n");
printf("/* Registration No.: BEE193041\n");
printf("/* Section No.: 1\n");
printf("/* Assignment No.: 5\n");
printf("/* Question No.# 01\n");
printf("/*=====================================================*/\n");

float(*ptr2func[ops])(float, float) = { add,sub,mul,div };


float a, b, result;
int choice;
printf("Enter your choice: 0 for sum, 1 for sub, 3 for mul, 4 for div:\n");
scanf_s("%d", &choice);
printf("enter two numbers\n");
scanf_s("%f %f", &a, &b);
printf("%f", ptr2func[choice](a, b));
float(*funct_ptr)(float, float);
funct_ptr = &add;
funct_ptr = &sub;
funct_ptr = &mul;
funct_ptr = &div;
switch (choice)
{
case'0':result = add(a, b); break;
case'1':result = sub(a, b); break;
case'2':result = mul(a, b); break;
case'3':result = div(a, b); break;
}
return 0;
}
Q#02
#include<stdio.h>
int abc(int *a);
int main()
{
printf("/*=================================================================\n");
printf("/* CSEE1122: Computer Programming\n");
printf("/* Student Name : Abdullah Abid\n");
printf("/* Registration No.: BEE193041\n");
printf("/* Section No.: 1\n");
printf("/* Assignment No.: 5\n");
printf("/* Question No.# 02\n");
printf("/*=====================================================*/\n");

int a, fact;
int *ptr;
printf("Enter a number:");
scanf_s("%d", &a);
ptr = &a;
fact = *ptr;
while (*ptr != 1)
{
fact = fact * abc(ptr);
}
printf("Factorial is %d\n:", fact);
return 0;
}
int abc(int *a)
{
*a = *a - 1;
return *a;
}
Q#03
#include<stdio.h>
#include <iostream>

using namespace std;

int vowelCount(char *sptr)


{
int count = 0;
while ((*sptr) != '\0') {
if (*sptr == 'a' || *sptr == 'e' || *sptr == 'i'
|| *sptr == 'o' || *sptr == 'u') {
count++;
}
sptr++;
}
return count;
}
int main()
{
printf("/*=================================================================\n");
printf("/* CSEE1122: Computer Programming\n");
printf("/* Student Name : Abdullah Abid\n");
printf("/* Registration No.: BEE193041\n");
printf("/* Section No.: 1\n");
printf("/* Assignment No.: 5\n");
printf("/* Question No.# 03\n");
printf("/*=====================================================*/\n");

char str[10];
printf("enter the name of a person\n", vowelCount(str));
gets_s(str);
printf("total vowels in a string:%d", vowelCount(str));
return 0;
}
Q#04
#include<stdio.h>
#include<string.h>
int main()
{
printf("/*=================================================================\n");
printf("/* CSEE1122: Computer Programming\n");
printf("/* Student Name : Abdullah Abid\n");
printf("/* Registration No.: BEE193041\n");
printf("/* Section No.: 1\n");
printf("/* Assignment No.: 5\n");
printf("/* Question No.# 04\n");
printf("/*=====================================================*/\n");

char data[100][100], search[50];


int i, n, c = 0;

printf("/How Many Names You Want \nto Add in 2-D Array/\n\nEnter Limit: ");
scanf_s("%d", &n);
printf("------------------------------------\n");
for (i = 0; i <= n; i++)
{
gets_s(data[i]);
}
printf("\nElement Present in 2-D Array are:\n");
printf("------------------------------------\n");
for (i = 0; i <= n; i++)
{
printf("\t%s\n", data[i]);
}
printf("\nEnter Name to be Searched: ");
gets_s(search);
for (i = 0; i <= n; i++)
{
if (strcmp(data[i], search) == 0)
{
c = 1;
break;
}
}
if (c == 1)
printf("\n%s Found at Position '%d'", data[i], i);
else
printf("\n%s NOT Present in Above Array", data[i]);
return 0;
}
Q#05

#include<stdio.h>

#define entries 10

int main()
{

char name[10]; // to get name as string

int ph_num[10]; // to get phone number

int search_num; // to get the num which is to be searched

int arr[entries][11]; // 2D array

int flag; // to tell search succesfull or not

for (int i = 0; i < entries; i++)

printf("enter name and phone number : \n");

gets_s(name);

scanf_s("%d", &ph_num);

arr[i][0] = ph_num[10];

for (int j = 0; name[j] != '\0'; j++)

arr[i][j + 1] = (int)name[j]; // converting char to int

}
for (int k = 0; ph_num[k] != '\0'; k++)

arr[i][k + 1] = (int)ph_num[k]; // converting char to int

}
}

printf("enter number to search : \n");

scanf_s("%d", &search_num);

for (int i = 0; i < entries; i++)

{
if (search_num == arr[i][0])

printf("******************** FOUND ******************\n");

for (int j = 1; arr[i][j] != '\0'; j++)

printf("%c", arr[i][j]);

flag = 1;

if (flag != 1)

printf("--- NOT FOUND --------------------------------\n");

return 0;

You might also like