Sei sulla pagina 1di 26

# Homework-3

## Name :- Bipul kumar

Sec. :- E4003. Course instructor:-Amandeep kaur
Roll no. :- RE4003A22 . Sub.- Foundation of computing
Regd. No.11010307
Course :- B.Tech (M.E.)

PART-A

Q1. Write a program to count the negative elements from the list using Arrays?
Ans 1.-

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int r[5],x,s=0;

for(x=0;x<=4;x++)

## printf(“\n enter a number=”);

scanf(“%d”,&r[x]);

if(r[x]<0)

s++;

## printf(“\n total negative elements are %d”,s);

getch();
}

Q2. How can you differentiate a lower level array from the higher level array?
Ans 2.-

(1).Lower Level Arrays:- This type of array are single dimensional .we can also represent
one-dimensional array. In this arrays we can only single data item include in a single box
of an array.

For eg.-

## When we will take int x[4]. It means we can write x[4]=”1234

So as per programming we cannot store the numerical value “1234” in a single array.
In other words we can say that we can only store our data in a single row or in single
column in an one-dimensional array.

(2).Higher Level Arrays:- In this Arrays which are more than two-dimensional array i.e.
two-dimensional, three-dimensional etc. These are called multi- dimensional arrays, in
which we can store multiple data items in a single row or in a column.

For eg.-

When we take take an array of character (char name[4][4]). So as per programming rule
we cannot store the word “chain” and “rain” can be stored in a single row/column of an
array. In other words we can say, only store our data in a multiple rows/columns.

Q3. Write a program that accepts an amount in figures and print that in words.

[Hint: - for an amount of Rs. 15200.50 it should output the string Rs. Fifteen
Thousand Two Hundred and Paisa Fifty only]
Ans 3.-

#include<stdio.h>

#include<conio.h>

void main()

{
clrscr();

## unsigned long int a,r,s,t,u,v,w,x,y,z;

int b,pslast,plast;

## printf("\nENTER AMOUNT(LESS THAN 100000 ONLY.ENTER RUPEES

ONLY):");

scanf("%ld",&a);

printf("\nENTER PAISA:");

scanf("%d",&b);

s=a/10;

t=s%10;

r=a%10;

u=a/100;

v=u%10;

w=a/1000;

x=w%10;

y=a/10000;

z=y%10;

pslast=b/10;

plast=b%10;

printf("\nRs.");

switch(z)

case 1:

if(x==1)
printf(" ELEVEN THOUSAND");

else if(x==2)

if(x==3)

if(x==4)

if(x==5)

if(x==6)

if(x==7)

if(x==8)

if(x==9)

if(x==10)

break;

case 2:

if(x==1)

else if(x==2)

if(x==3)

if(x==4)

if(x==5)

if(x==6)

if(x==7)

if(x==8)

if(x==9)

if(x==0)

break;

case 3:

if(x==1)

## printf(" THIRTY ONE THOUSAND");

else if(x==2)
printf(" THIRTY TWO THOUSAND");

if(x==3)

if(x==4)

if(x==5)

if(x==6)

if(x==7)

if(x==8)

if(x==9)

if(x==0)

break;

case 4:

if(x==1)

else if(x==2)

else if(x==3)

else if(x==4)

else if(x==5)

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

case 5:

if(x==1)

else if(x==2)

## printf(" FIFTY TWO THOUSAND");

else if(x==3)
printf(" FIFTY THREE THOUSAND");

else if(x==4)

else if(x==5)

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

case 6:

if(x==1)

else if(x==2)

else if(x==3)

else if(x==4)

else if(x==5)

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

case 7:

if(x==1)

else if(x==2)

else if(x==3)

## printf(" SEVENTY THREE THOUSAND");

else if(x==4)
printf(" SEVENTY FOUR THOUSAND");

else if(x==5)

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

case 8:

if(x==1)

else if(x==2)

else if(x==3)

else if(x==4)

else if(x==5)

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

case 9:

if(x==1)

else if(x==2)

else if(x==3)

else if(x==4)

## printf(" NINTY FOUR THOUSAND");

else if(x==5)
printf(" NINTY FIVE THOUSAND");

else if(x==6)

else if(x==7)

else if(x==8)

else if(x==9)

else if(x==0)

break;

default:

break;

if(z==0)

switch(x)

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:

break;

case 7:

break;

case 8:

break;

case 9:

break;

default:

break;
}

switch(v)

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:

## printf(" SIX HUNDRED AND");

break;

case 7:
printf(" SEVEN HUNDRED AND");

break;

case 8:

break;

case 9:

break;

default:

break;

## /*SECOND LAST EXTRACTION*/

switch(t)

case 2:

printf(" TWENTY");

break;

case 3:

printf(" THIRTY");

break;

case 4:

printf(" FOURTY");
break;

case 5:

printf(" FIFTY");

break;

case 6:

printf(" SIXTY");

break;

case 7:

printf(" SEVENTY");

break;

case 8:

printf(" EIGHTY");

break;

case 9:

printf(" NINTY");

break;

case 1:

if(r==1)

printf(" ELEVEN");

else if(r==2)

printf(" TWELVE");

else if(r==3)

printf(" THIRTEEN");

else if(r==4)
printf(" FOURTEEN");

else if(r==5)

printf(" FIFTEEN");

else if(r==6)

printf(" SIXTEEN");

else if(r==7)

printf(" SEVENTEEN");

else if(r==8)

printf(" EIGHTEEN");

else if(r==9)

printf(" NINETEEN");

break;

default:

break;

switch(r)

case 1:

if(t!=1)

printf(" ONE");

break;

case 2:

printf(" TWO");
break;

case 3:

printf(" THREE");

break;

case 4:

printf(" FOUR");

break;

case 5:

printf(" FIVE");

break;

case 6:

printf(" SIX");

break;

case 7:

printf(" SEVEN");

break;

case 8:

printf(" EIGHT");

break;

case 9:

printf(" NINE");

break;

case 0:

if(r==0&&s==0&&t==0&&u==0&&v==0&&w==0&&x==0&&y==0)
printf(" ZERO");

break;

default:

printf("INVALID INPUT!!!");

## printf(" RUPEES AND");

/*WORKING ON PAISA*/

switch(pslast)

case 2:

printf(" TWENTY");

break;

case 3:

printf(" THIRTY");

break;

case 4:

printf(" FOURTY");

break;

case 5:

printf(" FIFTY");

break;

case 6:

printf(" SIXTY");

break;
case 7:

printf(" SEVENTY");

break;

case 8:

printf(" EIGHTY");

break;

case 9:

printf(" NINTY");

break;

case 1:

if(plast==1)

printf(" ELEVEN");

else if(plast==2)

printf(" TWELVE");

else if(plast==3)

printf(" THIRTEEN");

else if(plast==4)

printf(" FOURTEEN");

else if(plast==5)

printf(" FIFTEEN");

else if(plast==6)

printf(" SIXTEEN");

else if(plast==7)

printf(" SEVENTEEN");
else if(plast==8)

printf(" EIGHTEEN");

else if(plast==9)

printf(" NINETEEN");

break;

default:

break;

/*LAST EXTRACTION*/

switch(plast)

case 1:

if(t!=1)

printf(" ONE");

break;

case 2:

printf(" TWO");

break;

case 3:

printf(" THREE");

break;

case 4:

printf(" FOUR");
break;

case 5:

printf(" FIVE");

break;

case 6:

printf(" SIX");

break;

case 7:

printf(" SEVEN");

break;

case 8:

printf(" EIGHT");

break;

case 9:

printf(" NINE");

break;

case 0:

if(pslast==0&&plast==0)

printf(" ZERO");

break;

default:

printf("\nINVALID PAISA");

## printf(" PAISA ONLY");

getch();

Q4. How will you initialize a three-dimensional array thread [3][2][3]? How will
you refer the first and last element in this array?
Ans 4.- To initialize a three dimensional thread [4][3][3] we have to follow the following
syntax:

data_type variable_name[dim1][dim2][dim3];

## Thus we can initialize the three dimensional thread as:

int x[4][3][3];

for(a=0;a<=3;i++)

for(b=0;b<=2;j++)

for(c=0;c<=2;k++)

scanf(“%d”,&x[a][b][c]);

}
The first element of this array can be referred as x[0][0][0] and last element can be
referred as a[4][3][3].

PART-B

Q5. Can an array of pointers to strings be used to collect strings from the
keyboard? If not, why not? How, if yes?
Ans 5.- In my point of view we can’t use array of pointers to strings to collect strings
from the keyboard. Because ,
In c language we don’t have a string data type but it implements strings as single-
dimension character array. A string is terminated by a null character ‘\0’which is at the
end of the string. The character arrays are declared one character longer than the largest
string they can hold. For eg.- If we declare a string of character ch[10],it has 11
characters the last one is 11.

## The index 0 refers to the first character, the index 1 to the

second, 2 to the third, and so fourth and so on. The end of a string is determined by
checking for null character.

Q6. Give an example of a structure declaration without the use of a tag? What are
Ans 6.-
struct empinfo
{
int eid;
char ename[30];
char emobno[10];
}
e1,e2 ;
Here, e1 and e2 are the variables of structure empinfo. Because, the declaration of
variables are along the structure, the tag is not compulsary. hence,we can write the above
code as,
struct
{
int eid;
char ename[30];
char emobno[10];
}
e1,e2;
It has a great advantage because the code gets easier to understand and read, and we can
create as many variables i.e. persons info but on the other hand since the structure has no
name we cannot create a variable of the structure in the main() function if needed.

## Q7.Create a structure to specify data on students given below:

Roll_no, name, deptt, course, year_of_passing
Assume that there are not more than 200 students in the college.
(a) Write a function to print names of all students who joined in a particular
year.
(b) Write a function to print the data of a student whose roll number is given.
Ans 7.-
#include<stdio.h>
#include<conio.h>
struct s1
{
int rollno;
char name[40];
char dept[40];
char course[40];
int yyyy;
}
void pn;
void pd;
void main()
{
int i;
struct s1 s[200];
clrscr();
for(i=0;i<200;i++)
{
printf(“\n enter students roll no.“);
scanf(“%d”, & s[i]rollno);
printf(“\n enter students name“);
scanf(“%s”, & s[i]name);
printf(“\n enter students department“);
scanf(“%s”, & s[i]dept);
printf(“\n enter students course“);
scanf(“%s”, & s[i]course);
printf(“\n enter the year of passing“);
scanf(“%d”, & yyyy);
}
pn();
pd();
getch();
}
void pn()
{
int year,j;
printf(“\n enter the year“);
scanf(“%d”, & year);
for(j=0;j<200;i++)
{
if(s[i] yyyy==year)
printf(“%s”, s[i]name);
}
}
void pd()
{
int r,k;
printf(“\n enter the roll number of the student whose data to be
shown: “);
scanf(“%d”, &r);
for(k=0;k<200;k++)
{
if(s[i].rollno==r)
{
printf(“\n name: %s”,s[i].name);
printf(“\n department is: %s”,s[i].dept);
printf(“\n course is: %s”,s[i].course);
printf(“\n year of joining: %s”,s[i].yyyy);
}
}
}