Uploaded by kamal khan

Lec 9 -1

advertisement
CSE 101
Computer Programming
What is an array ?
• Suppose you need 50 or 100 integers in a program, writing
them down one by one is a tedious job.
• In that case array comes to the rescue. Array is typically a set
of variables. We can define how many variables are there in
an array.
• Array is also 4 types, integer array, double array, float array
and finally character array.
Indexing of an array
• If we need an array of n variables and we want to name the
array num. Then we have to write
int num[50];
• Here num is the name just like normal integers and 50
defines the number of elements in the array.
Indexing an array
• To put a number suppose 10 in the first position of the array
we have to write num[0]=10.
• Remember that the positions start with 0, not 1. So to put
the value of 5th integer of num in x we have to write
x=num[4].
Array example,
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
Printf(“\n”);
printf("%d", myNumbers[2]);
return 0;
}
Changing Array Element
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
return 0;
}
Defining Array Elements
#include <stdio.h>
int main() {
// Declare an array of four integers:
int myNumbers[4];
// Add elements to it
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
printf("%d\n", myNumbers[0]);
return 0;
}
Example:
Suppose we have to input the values in an array and then print them in
reverse order. It can be done by the following code:
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}
for(i=0; i<n; i++)
{
sum += a[i];
}
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
Sorting array elements in ascending order
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
String
• An array of characters is called a string. If we want to input a name or
a sentence then we must keep it in a string.
• We have to make the array size bigger than needed otherwise data
will be lost.
• For name it is better to take the size 30 or more and for a sentence
we should take 60 or more.
• Remember that for string we use %s not %c and for taking input we
don’t need to use & sign.
String Example
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
Modify a string
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);
return 0;
}
Another way of creating string
#include <stdio.h>
int main() {
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
char greetings2[] = "Hello World!";
printf("%s\n", greetings);
printf("%s\n", greetings2);
return 0;
}
Null Character \0
Why do we include the \0 character at the end? This is known as the
"null terminating character", and must be included when creating
strings using this method. It tells C that this is the end of the string.
Example of defining string
#include <stdio.h>
int main() {
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
char greetings2[] = "Hello World!";
printf("%lu\n", sizeof(greetings));
printf("%lu\n", sizeof(greetings2));
return 0;
}
Example
Suppose you want to input your name and show your name in reverse
order.
Remember that when we use a space, the
string stops to take input.
So there is another function called gets()
Example
So we can take a complete sentence using gets() function and show in
reverse order.
Example
Suppose we want to find out whether a word is palindrome or not. For
example “madam” is palindrome so is “radar”
Problems
1. The input will be a sentence. You will have to find out how many
words are there in it.
2. Input will be 11 integers. You will have to find the maximum,
minimum and median of them.
3. Input will be a sentence. You will have to find out how many ‘a’ are
there in the sentence.
4. Input will be the roll number and marks of 10 students. You will
have to show them in ascending order.
Solve 1
Solve 2
Solve 3
Solve 4
Write a code to add two matrices.
Write a C program to find the transpose of a matrix
Write a C program to multiply to matrices
Download