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

POP_LAB_11_12

Principal of programming engineering 1st sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

POP_LAB_11_12

Principal of programming engineering 1st sem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C PROGRAMMING LAB 23ESCS11

PROGRAM-11
Write a C Program to input even & odd elements of an array in two separate arrays. The
program first finds the odd and even elements of the array. Then the odd elements of an
array are stored in one array and even elements of an array is stored in another array.
PROGRAM:
#include <stdio.h>
int main()
{
int n, i, j = 0, k = 0;
int a[50], even[50], odd[50];

printf("Enter the size of the array: ");


scanf("%d", &n);

printf("Enter the elements of the array: ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

// Finding odd and even elements and storing them in separate arrays
for (i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
{
even[j] = a[i];
j++;
}
else
{
odd[k] = a[i];
k++;
}
}

// Printing the even elements


printf("Even elements: ");
for (i = 0; i < j; i++)
{
printf("%d ", even[i]);
}
printf("\n");

// Printing the odd elements


printf("Odd elements: ");
for (i = 0; i < k; i++)
{
printf("%d ", odd[i]);
}

DSATM, Bangalore-82 Page 34


C PROGRAMMING LAB 23ESCS11

printf("\n");
}

OUTPUT:

Enter the size of the array: 5


Enter the elements of the array:
1
2
3
4
5
Even elements: 2 4
Odd elements: 1 3 5

DSATM, Bangalore-82 Page 35


C PROGRAMMING LAB 23ESCS11

PROGRAM-12
Write a C program to define a structure to represent a cricketer's information (name,
runs, average). Read the data corresponding to N Cricketer's in a structure array. The
space for the array of structures should be determined at run-time by user input.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

struct Cricketer
{
char name[50];
int runs;
float average;
};

int main()
{
int n;
printf("Enter the number of cricketers: ");
scanf("%d", &n);

struct Cricketer *cricketers = (struct Cricketer *)malloc(n * sizeof(struct Cricketer));


if (cricketers == NULL)
{
printf("Memory allocation failed. Exiting...\n");
return 1;
}
for (int i = 0; i < n; i++)
{
printf("Enter details for Cricketer #%d:\n", i+1);

printf("Name: ");
scanf("%s", cricketers[i].name);

printf("Runs: ");
scanf("%d", &cricketers[i].runs);

printf("Average: ");
scanf("%f", &cricketers[i].average);
}

printf("Cricketer Information:\n");
for (int i = 0; i < n; i++)
{
printf("Cricketer #%d\n", i+1);
printf("Name: %s\n", cricketers[i].name);
printf("Runs: %d\n", cricketers[i].runs);

DSATM, Bangalore-82 Page 36


C PROGRAMMING LAB 23ESCS11

printf("Average: %.2f\n", cricketers[i].average);


}
free(cricketers);
}

OUTPUT:
Enter the number of cricketers: 2
Enter details for Cricketer #1:
Name: Sachin
Runs: 10500
Average: 154
Enter details for Cricketer #2:
Name: Kohli
Runs: 10000
Average: 132
Cricketer Information:
Cricketer #1
Name: Sachin
Runs: 10500
Average: 154.00
Cricketer #2
Name: Kohli
Runs: 10000
Average: 132.00

DSATM, Bangalore-82 Page 37


C PROGRAMMING LAB 23ESCS11

Appendix

Common ‘C’ Errors

The most common errors can be broadly classified as follows

1. Programming errors
These errors are generated when typographical errors are made by users.
2. Compiler errors
These errors are detected by the compiler that make the program un-compilable.
3. Linker error
These errors are generated when the executable of the program cannot be generated.
This may be due to wrong function prototyping, incorrect header files.
4. Execution error
These errors occur at the time of execution. Looping and arithmetic errors falls
under this category.
5. Logical errors
These errors solely depend on the logical thinking of the programmer and are easy to
detect if we follow the line of execution and determine why the program takes that
path of execution.

List of some common programming errors


1. Misuse of the Include Guard.
2. Switch statements without break.
3. Wrong usage of postfix and prefix operator (diff between i++ and ++i)
4. Undeclared and Uninitialized Variables and functions.
5. Trying to include "INCORRECT" header function.
6. Returning a value in a void function.
7. Confusing the name of an array with the contents of the first element.
8. ‘C’ string array Errors - Arr[10] = Arr[0] to Arr[9]. Trying to access Arr[10]
element.
9. Using "=" (assignment operator) instead of "= =" (comparison operators) scanf()
without '&'
and wrong format.(IN C)
10. Trying to divide a number by Zero.
11. Poor Loop Exiting comparisons will tend to either loop being not executed at all
or goes to
an infinite loop.
12. Not using string functions and treating the strings are integer. Say trying to compare string
by

(string1= = string2), rather than using strcmp command


13. ‘C’ String not terminated by '\0'- Null character
14. Mismatched "{" or IF-ELSE statements or for that matter any looping statement.
15. Missing “;” at the end of every statement except control statements and function
definition.

DSATM, Bangalore-82 Page 38


C PROGRAMMING LAB 23ESCS11

Some Examples of Common Mistakes


1. Forgetting to put a break in a switch statement
Remember that C does not break out of a switch statement if a case is encountered. For
example:
int x = 2;
switch(x)
{
case 2: printf("Two\n");
case 3: printf("Three\n");
}
prints output:
Two
Three
Put a break to break out of the switch:
int x = 2;
switch(x)
{
case 2: printf("Two\n");
break;
case 3: printf("Three\n");
break; /* not necessary, but good if additional cases are added later
*/
}
2. Using = instead of ==
C's = operator is used exclusively for assignment and returns the value assigned. The ==
operator is used exclusively for comparison and returns an integer value (0 for false, not
0 for true). Because of these return values, the C compiler often does not flag an error
when = is used when one really wanted an ==. For example:
int x = 5;
if ( x = 6 )
printf("x equals 6\n");
This code prints out x equals 6! Why? The assignment inside the if sets x to 6 and returns the value 6
to the if. Since 6 is not 0, this is interpreted as true.
One way to have the compiler find this type of error is to put any constants (or any r-value
expressions) on the left side. Then if an = is used, it will be an error:
if ( 6 = x)

DSATM, Bangalore-82 Page 39


C PROGRAMMING LAB 23ESCS11

3. scanf() errors
There are two types of common scanf() errors:
1. Forgetting to put an ampersand (&) on arguments
scanf() must have the address of the variable to store input into. This means
that often the ampersand address operator is required to compute the addresses.
Here's an example:
scanf("%d", &x); /* & required to pass address to scanf() */
scanf("%30s", st); /* NO & here, st itself points to variable! */
2. Using the wrong format for operand
C compilers do not check that the correct format is used for arguments
of a scanf() call. The most common errors are using the %f format for
doubles (which must use the %lf format) and mixing up %c and %s
for characters and strings.
4. Size of arrays
Arrays in C always start at index 0. This means that an array of 10 integers defined as:
int a[10];
has valid indices from 0 to 9 not 10! It is very common for students go one too far in an
array. This can lead to unpredictable behavior of the program.
5. Integer division
C uses the / operator for both real and integer division. It is important to understand how
C determines which it will do. If both operands are of an integal type, integer division is
used, else real division is used. For example:
double half = 1/2;
This code sets half to 0 not 0.5! Why? Because 1 and 2 are integer constants. To fix this,
change at least one of them to a real constant.
double half = 1.0/2;
If both operands are integer variables and real division is desired, cast one of the variables
to double (or float).
int x = 5, y = 2;
double d = ((double) x)/y;
6. Loop errors
In C, a loop repeats the very next statement after the loop statement. The code:
int x = 5;
while( x > 0 );
x--;

DSATM, Bangalore-82 Page 40


C PROGRAMMING LAB 23ESCS11

is an infinite loop. Why? The semicolon after the while defines the statement to repeat as
the null statement (which does nothing). Remove the semicolon and the loop works as
expected.
Another common loop error is to iterate one too many times or one too few.
Check loop conditions carefully!
7. Not using prototypes
Prototypes tell the compiler important features of a function: the return type and the
parameters of the function. If no prototype is given, the compiler assumes that the
function returns an int and can take any number of parameters of any type.
One important reason to use prototypes is to let the compiler check for errors
in the argument lists of function calls. However, a prototype must be used if the function
does not return an int. For example, the sqrt() function returns a double, not an int. The
following code:
double x = sqrt(2);
will not work correctly if a prototype:
double sqrt(double);
does not appear above it. Why? Without a prototype, the C compiler assumes that sqrt()
returns an int. Since the returned value is stored in a double variable, the compiler inserts
code to convert the value to a double. This conversion is not needed and will result in the
wrong value.
The solution to this problem is to include the correct C header file that contains
the sqrt() prototype, math.h. For functions you write, you must either place the prototype
at the top of the source file or create a header file and include it.
8. String Errors
1. Confusing character and string constants
C considers character and string constants as very different things. Character
constants are enclosed in single quotes and string constants are enclosed in
double quotes. String constants act as a pointer to the actually string. Consider
the following code:
char ch = 'A'; /* correct */
char ch = "A"; /* error */
The second line assigns the character variable ch to the address of a string
constant. This should generate a compiler error.

2. Comparing strings with ==


Never use the == operator to compare the value of strings! Strings are char
arrays. The name of a char array acts like a pointer to the string (just like other
types of arrays in C). So what? Consider the following code:
char st1[] = "abc";
char st2[] = "abc";

DSATM, Bangalore-82 Page 41


C PROGRAMMING LAB 23ESCS11

if ( st1 == st2 )
printf("Yes");
else
printf("No");
This code prints out No. Why? Because the == operator is comparing the
pointer values of st1 and st2, not the data pointed to by them. The correct way
to compare string values is to use the strcmp() library function. (Be sure to
include string.h) If the if statement above is replaced with the following:
if ( strcmp(st1,st2) == 0 )
printf("Yes");
else
printf("No");
The code will print out Yes. For similar reasons, don't use the other relational
operators (<,>, etc.) with strings either. Use strcmp() here too.

DSATM, Bangalore-82 Page 42


C PROGRAMMING LAB 23ESCS11

VIVA QUESTIONS
1. What is a Computer?
2. What is a compiler?
3. Differentiate object & executable file?
4. What is execution?
5. What is compilation?
6. What are the uses of Internet?
7. Explain different parts of the computer?
8. Define tracing, debugging?
9. What are the differences between RAM and ROM?
10. What are bits? What is a byte?
11. What is booting?
12. What is a source program?
13. Explain the structure of C program with an example.
14. Who is the father of “C” Computer?
15. Steps to execute a program?
16. Which are input & output statements?
17. Explain the different types of IF statements
18. Differentiate between break and continue
19. What are operators? List out different operators?
20. List out fundamental data types in C.
21. What do you mean by type conversions?
22. Why looping is necessary?
23. What is the syntax of a function declaration & function definition?
24. Define recursion & its application?
25. What is an array? How are they declared in ‘C’? What are the rules to be followed while
using arrays?
26. Explain the single and multi-dimensional arrays?
27. Explain the different categories of functions?
28. What is a string? What is a string manipulation library functions?
29. What are preprocessor directive?
30. List out different header files?
31. What is a parallel program?
32. What is the use of clrscr ()?
33. How to execute a parallel program?
34. What is searching & sorting? Logic of bubble sort, binary search?
35. What is divide & conquer methods?
36. What is memory allocation?
37. What is a pointer?
38. Explain format specifier, escape sequence?
39. Applications of arrays, looping, conditional statements?
40. Can we have main function within a main function?
41. Difference between object oriented & procedure oriented programs?
42. Logic of all programs?
43. Explain flowchart & algorithm?

DSATM, Bangalore-82 Page 43

You might also like