week6_1D_arrays
week6_1D_arrays
• Programming : From Turtle to C.
• Data Types in C, Operators. Input and the Output.
• Modifying the control flow in Programs if-else,
Week 1-5
switch, loops : while, do-while, for.
• Implementing numerical methods using a
C-program.
CS1100 – Introduction to Programming
• Programming : From Turtle to C.
• Data Types in C, Operators. Input and the Output.
• Modifying the control flow in Programs if-else,
Week 1-5
switch, loops : while, do-while, for.
• Implementing numerical methods using a
C-program.
• One-dimensional Arrays in C. Week 6
Programming for Real life problems
Here are some real life problems that we may want to solve using
computers.
• Given the marks obtained by students in a class, print out the
marks in the non-decreasing order. That is, smallest marks
first.
Programming for Real life problems
Here are some real life problems that we may want to solve using
computers.
• Given the marks obtained by students in a class, print out the
marks in the non-decreasing order. That is, smallest marks
first.
• Given a road map of India find the shortest path from Main
gate of IIT Madras to Dharwad.
Programming for Real life problems
Here are some real life problems that we may want to solve using
computers.
• Given the marks obtained by students in a class, print out the
marks in the non-decreasing order. That is, smallest marks
first.
• Given a road map of India find the shortest path from Main
gate of IIT Madras to Dharwad.
• Given the positions, velocities and masses of stars, determine
their state 1 million years from today.
Programming for Real life problems
Here are some real life problems that we may want to solve using
computers.
• Given the marks obtained by students in a class, print out the
marks in the non-decreasing order. That is, smallest marks
first.
• Given a road map of India find the shortest path from Main
gate of IIT Madras to Dharwad.
• Given the positions, velocities and masses of stars, determine
their state 1 million years from today.
Difficulties : Size of the input data is huge !
Programming for Real life problems
Here are some real life problems that we may want to solve using
computers.
• Given the marks obtained by students in a class, print out the
marks in the non-decreasing order. That is, smallest marks
first.
• Given a road map of India find the shortest path from Main
gate of IIT Madras to Dharwad.
• Given the positions, velocities and masses of stars, determine
their state 1 million years from today.
Difficulties : Size of the input data is huge !
See example 1 : defining a variable for each mark is not feasible
What is an array?
• Declaration :
data-type array-name[array-size];
What is an array?
• Declaration :
data-type array-name[array-size];
• int marks[7];
What is an array?
• Declaration :
data-type array-name[array-size];
• int marks[7];
• char name[10];
• float score[1000];
What is an array?
• Declaration :
data-type array-name[array-size];
• int marks[7];
• char name[10];
• float score[1000]; - defines 1000 variables!
What is an array?
• Declaration :
data-type array-name[array-size];
• int marks[7];
• char name[10];
• float score[1000]; - defines 1000 variables!
• the value of marks[2] is 75.
• new values can be assigned to elements
marks[3] = 36;
Storing Arrays
#include<stdio.h>
main() {
const int MAX_MARKS = 25;
const int NUM_STUDENTS = 56;
int marksCount[MAX_MARKS+1];
int i, currMarks;
for (i=1; i<= NUM_STUDENTS; i++) {
printf("Enter the marks for Rollnumber %d\t", i);
scanf("%d", &currMarks);
marksCount[currMarks]++;
}
}
Counting number of students who scored marks-i
#include<stdio.h>
main() {
const int MAX_MARKS = 25;
const int NUM_STUDENTS = 56;
int marksCount[MAX_MARKS+1];
int i, currMarks;
for (i=1; i<= NUM_STUDENTS; i++) {
printf("Enter the marks for Rollnumber %d\t", i);
scanf("%d", &currMarks);
marksCount[currMarks]++;
}
}
#include<stdio.h>
main() {
const int MAX_MARKS = 25;
const int NUM_STUDENTS = 56;
int marksCount[MAX_MARKS+1];
int i, currMarks;
for (i=1; i<= NUM_STUDENTS; i++) {
printf("Enter the marks for Rollnumber %d\t", i);
scanf("%d", &currMarks);
marksCount[currMarks]++;
}
}
int i, currMarks;
int sum;
P(x) = a0 + a1 x + a2 x2 + . . . + an xn
P(x) = a0 + a1 x + a2 x2 + . . . + an xn
P(x) = a0 + a1 x + a2 x2 + . . . + an xn
#include <stdio.h>
#include <math.h>
main() {
int x, n, i;
int coeff[20]; // maximum degree = 20.
int value = 0;
int product = 1;
printf("%d\n", value);
}
Evaluating a polynomial
#include<stdio.h>
main() {
int x, n, i;
int coeff[20]; // maximum degree = 20.
int value;
printf("%d\n", value);
}
Evaluating a polynomial
Character arrays
char name[20];
char name[20];
#include<stdio.h>
int main() {
char name[20] = "AVANI";
int i;
User input two strings s1, s2. Determine if s1 and s2 are the same.
Compare two strings
User input two strings s1, s2. Determine if s1 and s2 are the same.
User input two strings s1, s2. Determine if s1 and s2 are the same.
• malayalam
• neveroddoreven
• dontnod
Palindromes
• malayalam
• neveroddoreven
• dontnod