MA 511: Computer Programming
Lecture 4:
http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html
Partha Sarathi Mandal
psm@iitg.ernet.ac.in
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Library Functions
math.h : ceil(d), floor(d), sin(d), cos(d), tan(d),
sqrt(d), cosh(d), exp(d), fabs(d), log(d),
pow(d1,d2),…
stdlib.h : rand(), srand(u), abs(i), tolower(c),
toupper(c),…..
stdio.h : printf(), scanf(), getchar(), putchar(),..
string.h : strcpy(s1, s2), strcmp(s1,s2), strlen(s1)…
Random number generator
int seed, s;
double r;
seed = 10000; // choose a seed value
srand(seed); //initialize random number generator
s=rand(); // random integer
r=((double)rand() / ((double)(RAND_MAX)+(double)(1)));
// random number in [0, 1)
Where RAND_MAX may be the largest positive integer the
architecture can represent.
Assignments
1. Write a c-code for generating n arbitrary
(random) points in a square of size n, then
identity and report which of them are placed
i) inside ii) outside and iii) on a given circle (center and
radius are given as input parameters).
2. over the above given points calculate
distance between all pair of points and
report the maximum and minimum
distances.
Input & Output
o Following functions permits the transfer of information
between computer and standard input and output.
- getchar, putchar, scanf, printf
o getchat & putchar
Example: single character
char c;
printf("Enter any char value: ");
c = getchar();
printf("the corresponding uppercase char : ");
putchar(toupper(c));
scanf: String reading
• char text[80];
• scanf(“ %*^\n+”, text);
/* writr a c program to find the length of the string using strlen() function */
#include<stdio.h>
#include<string.h>
main(){
char name[100];
int length;
printf("Enter the string");
scanf("%s", name);
length=strlen(name);
printf("\nNumber of characters in the string is=%d\n",length);
}
• scanf statement has a draw back it just terminates the statement as soon as it finds
a blank space, suppose if we type the string New York then only the string new will
be read and since there is a blank space after word “New” it will terminate the
string.
scanf: String reading
• char text[80];
• scanf(“ %*^\n+”, text);
/* writr a c program to find the length of the string using strlen() function */
#include<stdio.h>
#include<string.h>
main(){
char name[100];
int length;
printf("Enter the string");
scanf("%[^\n]", name);
length=strlen(name);
printf("\nNumber of characters in the string is=%d\n",length);
}
scanf
scanf(“%3d %3d %3d”, &a, &b, &c)
Input: 1 2 3
Output 1 2 3
Input 123 234 456
Output 123 234 456
Input 123234345
Output 123 234 345
Input 1234 2345 5
Output 123 4 234
scanf(“%3d, %3d, %3d”, &a, &b, &c)
scanf
• float f
• short ix, iy;
• long lx, ly;
• double dx, dy;
• scanf(“%4f %hd %ld %lf ”, &f, &ix, &lx, &dx);
• scanf(“%3ho %7lx %15le”, &iy, &ly, &dy);
o: octal x: hexadecimal e: double-precision
h: short l: long
Strings manipulation ?
• We cannot manipulate strings since C does not provide
any operators for string. For instance we cannot assign
one string to another directly.
String=“xyz”;
string1=string2;
• Are not valid.
• To copy the chars in one string to another string we
may do so on a character to character basis.
• char a = ‘x’, b = ‘3’, c = ‘#’, text*18+ = “guwahati”;
• Are valid
String operations (string.h)
• Length (number of characters in the string).
– length=strlen(name);
• Concatentation (adding two are more strings)
– strcpy(string1,”sri”);
strcpy(string2,”Bhagavan”);
Printf(“%s”,strcat(string1,string2);
• Comparing two strings.
– strcmp(string1,string2)
• Copy(copies one string over another)
• Exercise: Substring (Extract substring from a given
string)
Example
Read a string than replace each character with an equivalent encoded character
char line[80];
int i;
printf(“Type a line of text\n”);
scanf(“%*^\n+”, line);
for(i=0; line[i+ != ‘\o’; ++i){
)
if(((line[i+>=‘0’) && (line[i+<‘9’))||((line[i+>=‘A’) && (line[i+<‘Z’)) || ((line[i+>=‘a’) && (line[i+<‘z’))
putchar(line[i]+1);
else if (line[i+ == ‘9’)
putchar(‘0’);
else if (line[i+ == ‘Z’)
putchar(‘A’);
else if (line[i+ == ‘z’)
putchar(‘a’);
else
putchar(‘.’);
}
Input: IIT Guwahati, 781039, Assam, India.
Output: JJU.Hvxbibuj..892140..Bttbn….Joejb.
ASCII
Character ASCII value
0 48
9 57
A 65
Z 90
a 97
z 122
Assignments
1. Read a string of alphabets (a to z or A to Z)
than replace each character with an
equivalent encoded character as follows.
2. A or a – 1
3. B or b – 2
26. Z or z- 26
Assignment
Solve the following algebraic Equation:
X5+3x2-10=0