0% found this document useful (0 votes)
12 views2 pages

Cse115 Lab Manual 14 1D Array

Uploaded by

tasin.nextlab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
12 views2 pages

Cse115 Lab Manual 14 1D Array

Uploaded by

tasin.nextlab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

CSE 115 Lab on Strings and 1D Array – Ara2

1. C Program that prints the odd & the even numbers in an array separately:
#include <stdio.h> printf("Even numbers in the array are:");
void main() for (i = 0; i < num; i++) {
{ if (array[i] % 2 == 0)
int i, num; printf("%d \t", array[i]);
printf("Enter size of array:"); }
scanf("%d", &num); printf("\nOdd numbers in the array
int array[num]; are:");
for (i = 0; i < num; i++) {
printf("Enter its elements\n"); if (array[i] % 2 != 0)
for (i = 0; i < num; i++) printf("%d \t", array[i]);
scanf("%d", &array[i]); }
}//main

Try yourself 1: Write a program that prints the no. of odd & no. of even numbers in an array.
2. C Program to find the largest value in a float type array:
#include <stdio.h>
void main()
{
int i,n;
printf("No. of elements: ");
scanf("%d",&n);
float arr[n], max;
printf("Enter %d numbers: ",n);
for(i=0; i<n; ++i) // fill up array by user inputs
scanf("%f",&arr[i]);
max = arr[0]; //initially assume arr[0] is the max
for(i=1; i<n; ++i) {
if(max < arr[i]) //update max if arr[i] > current value of max
max=arr[i];
}
printf("maximum=%.2f",max);
}

3. C Program to read two arrays from user, add them, and then output their sum:
#include<stdio.h> printf("Enter 2nd array:");
void main() for (i=0; i<n; i++)
{ scanf("%d",&b[i]);
int i, n;
printf("No. of elements: "); //compute sum of two arrays
scanf("%d",&n); for(i=0; i<n; i++)
int a[n],b[n],c[n]; {
c[i]=a[i]+b[i];
printf("Enter 1st array:"); printf("\n %d+ %d=%d",a[i],b[i],c[i]);
for (i=0; i<n; i++) }
scanf("%d",&a[i]); }

Try yourself 3: Write a program reads two arrays from user and then output their product.
4. C Program to read an array from user and an index and then delete the element in that index of array:
#include<stdio.h> //shift each array element one cell
void main() //left, starting from index k+1
{ for(i=k; i<num-1; i++)
int num, i, k; arr[i] = arr[i+1];
printf("\nEnter no of elements :");
scanf("%d", &num); num--; // decrease No of elements
int arr[num];
//Read elements in an array printf("Array after deleting the
printf("\nEnter %d numbers :", num); element at index: %d\n", k);
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
scanf("%d", &arr[i]); printf("%d ", arr[i]);
printf("Index of element to delete:"); }
scanf("%d", &k);

5. C Program to find the number of spaces in a string (character array):


#include<stdio.h>
void main()
{
char s[100];//assume that user won't enter a string of size > 100
int i,numSpace=0;//counter to count #of spaces
printf("Enter a string:");
gets(s); //read string from user
for(i=0; s[i]!='\0'; i++) {
if(s[i] == ' ') numSpace++;
}
printf("%d spaces\n", numSpace);
}

Assignment:
1. Write a program that deletes the first element in an array which matches a search key.
2. Write a program that deletes all the elements in an array which match a search key.
3. Write a C program to print the 2nd largest & 2nd smallest elements of an array.
4. Write a program that reads the size and elements of an float array from user and then computes the
average of the numbers in it and prints it. Then it should compute number of elements which are
greater than average and prints those elements. Sample input/output:
Enter array size: 5
Enter elements: 12 13 16 15 14
Average = 14.000000, The elements greater than average are: 16, 15
5. Write C program to check if an input string is a palindrome (e.g. “madam”, “dad”, etc.) or not.
6. Write C program to count the number of capital letters and the number of small letters in an input string
and print those numbers.

You might also like