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

Exercise 1: Using Pointers

1. The document contains 6 exercises on C programming concepts like pointers, structures, functions, and dynamic memory allocation using malloc. 2. The exercises cover using pointers to pass arguments to functions, defining structures to store related data, allocating dynamic memory for arrays using malloc, and defining functions to operate on structures. 3. Example code is provided to demonstrate passing structures to functions, reading/writing structure fields using pointers, defining structures with different data types as members, and allocating memory for arrays of structures using malloc.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Exercise 1: Using Pointers

1. The document contains 6 exercises on C programming concepts like pointers, structures, functions, and dynamic memory allocation using malloc. 2. The exercises cover using pointers to pass arguments to functions, defining structures to store related data, allocating dynamic memory for arrays using malloc, and defining functions to operate on structures. 3. Example code is provided to demonstrate passing structures to functions, reading/writing structure fields using pointers, defining structures with different data types as members, and allocating memory for arrays of structures using malloc.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C Programs

=========
Exercise 1: Using Pointers
//pointers
#include<stdio.h>
void main()
{
int ia,*iptr
ia=!"
iptr=#ia
print$(%&d'n%,ia)
print$(%&p'n%,#ia)
print$(%&p'n%,iptr)
print$(%&p'n%,#iptr)
print$(%&d'n%,*iptr)
(
Exercise 2: Using Pointers and Funtions
#include<stdio.h>
void )n*oman( int n )
int main( void )
{
int n
print$( %+nter the ,ra-ic num-er. % )
scan$( %&d%, #n )
print$( %)n *oman, % )
)n*oman( n )
print$(%'n%)
return(")
( /*//main( )/////////*/
void )n*oman( int n ) /* converts ara-ic to roman */
{
int i, v0 1 = { !, 2, 3, 4, !", 2", 3", 4", !"",
2"", 3"", 4"", !""", 4444 (
char *r0 1 = { %)%, %)5%, %5%, %)6%, %6%, %67%, %7%, %6C%, %C%,
%C8%, %8%, %C9%, %9% (
:hile ( n )
{
$or( i=" v0i1<=n ;;i )

//i
n /= v0i1
print$( %&s%, r0i1 )
(
(
Exercise 3: Pointers and functions
#include <stdio.h>
void addr(int a, int * -)
int main(void) {
int <
int *=
<=!
==#<
print$(%,ddress o$ < = &p, value o$ < = &d'n%, #<, <)
print$(%,ddress o$ = = &p, value o$ = = &p, value o$ *= = &d'n%, #=, =, *=)
addr(4,=)
(
void addr(int a, int *-){
print$(%,ddress o$ a = &p, value o$ a = &d'n%, #a, a)
print$(%,ddress o$ - = &p, value o$ - = &p, value o$ *- = &d'n%, #-, -, *-)
(
Exercise 4: Pointers and functions
#include <stdio.h>
#de$ine >)?+ @
void cp)nt,rra=(int *a, int *-, int n)
/*)t copies n integers starting at - into a*/
{
$or(n>"n//)
*a;;=*-;;
(
void print)nt,rra=(int a01, int n)
/* n is the num-er o$ elements in the arra= a.
* Ahese values are printed out, $ive per line. */
{
int i
$or (i=" i<n ){
print$(%'t&d %, a0i;;1)
i$ (i&3==")
print$(%'n%)
(
print$(%'n%)
(
int get)nt,rra=(int a01, int nma<, int sentinel)
/* )t reads up to nma< integers and stores then in a sentinel
* terminates input. */
{
int n = "
int temp
do {
print$(%+nter integer 0&d to terminate1 . %, sentinel)
scan$(%&d%, #temp)
i$ (temp==sentinel) -reaB
i$ (n==nma<)
print$(%arra= is $ull'n%)
else
a0n;;1 = temp
(:hile (!)
return n
(
int main(void){
int <0>)?+1, n<
int =0>)?+1, n=
print$(%*ead the < arra=.'n%)
n< = get)nt,rra=(<,>)?+,")
print$(%Ahe < arra= is.'n%)
print)nt,rra=(<,n<)
print$(%*ead the = arra=.'n%)
n= = get)nt,rra=(=,>)?+,")
print$(%Ahe = arra= is.'n%)
print)nt,rra=(=,n=)
cp)nt,rra=(<;C,=;D,2)

print$(%Printing < a$ter having copied 2 elements'n%
%$rom = starting at =0D1 into < starting at <0C1'n%)
print)nt,rra=(<,n<)
(
Exercise 5: Function with return type and parameters
#include <stdio.h>
void sEuare!(void)
void sEuareC(int i)
int sEuareD(void)

int sEuare2(int i)
int area(int -, int h)
int main (void) {
sEuare!()
sEuareC(F)
print$(%Ahe value o$ sEuareD() is &d'n%, sEuareD())
print$(%Ahe value o$ sEuare2(3) is &d'n%, sEuare2(3))
print$(%Ahe value o$ area(D,F) is &d'n%, area(D,F))
(
void sEuare!(void){
int <
print$(%Please enter an integer > %)
scan$(%&d%, #<)
print$(%Ahe sEuare o$ &d is &d'n%, <, <*<)
(
void sEuareC(int i){
print$(%Ahe sEuare o$ &d is &d'n%, i, i*i)
(
int sEuareD(void){
int <
print$(%Please enter an integer > %)
scan$(%&d%, #<)
return (<*<)
(
int sEuare2(int i){
return (i*i)
(
int area(int -, int h){
return (-*h)
(
Exercise 6: Using tructures
#include <stdio.h>
struct 8istance{
int $eet
$loat inch
(d!,dC,sum
int main(){
print$(%!st distance'n%)
print$(%+nter $eet. %)
scan$(%&d%,#d!.$eet) /* input o$ $eet $or structure varia-le d! */
print$(%+nter inch. %)
scan$(%&$%,#d!.inch) /* input o$ inch $or structure varia-le d! */
print$(%Cnd distance'n%)
print$(%+nter $eet. %)
scan$(%&d%,#dC.$eet) /* input o$ $eet $or structure varia-le dC */
print$(%+nter inch. %)
scan$(%&$%,#dC.inch) /* input o$ inch $or structure varia-le dC */
sum.$eet=d!.$eet;dC.$eet
sum.inch=d!.inch;dC.inch
i$ (sum.inch>!C){ //)$ inch is greater than !C, changing it to $eet.
;;sum.$eet
sum.inch=sum.inch/!C
(
print$(%>um o$ distances=&d'G/&.!$'%'n%,sum.$eet,sum.inch)
/* printing sum o$ distance d! and dC */
return "
(
Exercise !: Using tructures with pointers
#include <stdio.h>
struct name{
int a
$loat -
(
int main(){
struct name *ptr,p
ptr=#p /* *e$erencing pointer to memor= address o$ p */
print$(%+nter integer. %)
scan$(%&d%,#(*ptr).a)
print$(%+nter num-er. %)
scan$(%&$%,#(*ptr).-)
print$(%8ispla=ing. %)
print$(%&d&$%,(*ptr).a,(*ptr).-)
return "
(
/* (*ptr).a is same as ptr/>a
(*ptr).- is same as ptr/>- */
Exercise ": using ma##oc
#include <stdio.h>
#include<stdli-.h>
struct name {
int a
$loat -
char c0D"1
(
int main(){
struct name *ptr
int i,n
print$(%+nter n. %)
scan$(%&d%,#n)
ptr=(struct name*)malloc(n*siHeo$(struct name))
/* ,-ove statement allocates the memor= $or n structures :ith pointer ptr pointing to -ase address
*/
$or(i="i<n;;i){
print$(%+nter string, integer and $loating num-er respectivel=.'n%)
scan$(%&s&d&$%,(ptr;i)/>c,#(ptr;i)/>a,#(ptr;i)/>-)
(
print$(%8ispla=ing )n$romation.'n%)
$or(i="i<n;;i)
print$(%&s't&d't&.C$'n%,(ptr;i)/>c,(ptr;i)/>a,(ptr;i)/>-)
return "
(
Exercise $: tuctures and functions
#include <stdio.h>
struct student{
char name03"1
int roll
(
void 8ispla=(struct student stu)
/* $unction protot=pe should -e -elo: to the structure declaration other:ise compiler sho:s error */
int main(){
struct student s!
print$(%+nter studentGs name. %)
scan$(%&s%,s!.name)
print$(%+nter roll num-er.%)
scan$(%&d%,#s!.roll)
8ispla=(s!) // passing structure varia-le s! as argument
return "
(
void 8ispla=(struct student stu){
print$(%Iutput'nJame. &s%,stu.name)
print$(%'n*oll. &d'n%,stu.roll)
(

You might also like