- #include<stdio.h>
- #define size 5
- int stack[size];
- int tos= -1;
- void push();
- void pop();
- void display();
- int main()
- {
- int ch,c;
- do
- {
- printf("\n1. push\n 2.pop \n3. exit\n Enter your choice:\t");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- push();
- printf("\nstack:\t");
- display();
- break;
- case 2:
- pop();
- printf("\nstack:\t");
- display();
- break;
- case 3:
- break;
- default:
- printf(" invalid choice!");
- }
- printf("\nreopen the series:?press 1:");
- scanf("%d",&c);
- } while(c==1);
- return 0;
- }
- void push()
- {
- int data;
- if (tos,(size-1))
- { tos=tos+1;
- printf("\n Enter data:");
- scanf("%d",&data);
- stack[tos]=data;
- if(tos==(size-1))
- printf("\nstack is full");
- }
- else
- printf("\n overflow");
- }
- void pop()
- {
- int data;
- if (tos==-1)
- printf("\n underflow");
- else
- {
- data=stack[tos];
- printf("\n popped=%d",data);
- tos=tos-1;
- }
- }
- void display()
- {
- int i;
- if(tos==-1)
- printf("stack is empty.");
- else
- {
- for(i=tos;i>=0;i--)
- {
- printf("\n%d",stack[i]);
- }
- }
- }
May 23, 2020
Array, Data Structure, Programming in C
No comments
May 20, 2020
Array, Programming in C
1 comment
Problem: WAP to perform following actions on an array entered by
the user :
i) Print the even-valued elements
ii) Print the odd-valued elements
iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options. The
menu should also include options to re-enter array and to quit the program.
Source Code:
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- void even(); //User defined function to check the even elements of the array
- void odd(); //User defined function to check the odd elements of the array
- void sum_and_avg(); //User defined function to find the sum and average of elements of the array
- void max_and_min(); //User defined function to the maximum and minimum element of the array
- void remove_duplicates(); //User defined function to remove duplicate elements of the array
- void reverse(); //User defined function to reverse the array
- int main()
- {
- int option; //Declaring variables
- char choice; //Declaring variable
- do{
- //Displaying Menu options
- printf("\n _______________MENU__________");
- printf("\n 1. Print the even-valued elements of the array");
- printf("\n 2. Print the odd-valued elements of the array");
- printf("\n 3. Calculate and print the sum and average of the elements of array");
- printf("\n 4. Print the maximum and minimum element of array");
- printf("\n 5. Remove the duplicates from the array");
- printf("\n 6. Print the array in reverse order");
- printf("\n 7. Exit");
- printf("\n\n Enter Your Choice : ");//Inputting the choice from the user
- scanf("%d", &option);
- switch(option)//Checking the entered choice
- {
- case 1:
- even();//Calling the function
- break;
- case 2:
- odd();//Calling the function
- break;
- case 3:
- sum_and_avg();//Calling the function
- break;
- case 4:
- max_and_min();//Calling the function
- break;
- case 5:
- remove_duplicates();//Calling the function
- break;
- case 6:
- reverse();//Calling the function
- break;
- case 7:
- printf("Exiting Program........");
- exit(1);
- default:
- printf("\n Invalid Choice !");
- break;
- }
- printf("Do you want to continue (Y/N)? ");
- choice=getch();
- }
- while(choice=='Y' || choice=='y');//repeat the loop until user wants to exit the program
- }
- //Defining the function even()
- void even()
- {
- char op;
- int i, num,even=0;//Initializations and declaration of variables
- printf("\n Enter the size of an array : ");//Inputtin the size of the array
- scanf("%d", &num);
- int array[num];//Initializing array of same size
- printf("\n Enter the elements of the array : ");//Inoutting the elements in the array
- for (i = 0; i < num; i++)
- {
- scanf("%d", &array[i]);
- }
- printf("\n Even numbers in the array are : ");//Displaying the even elements of the array
- for (i = 0; i < num; i++)
- {
- if (array[i] % 2 == 0)
- {
- even++;
- printf("%d \t", array[i]);
- }
- }
- }//End of function even()
- //Defining the function odd()
- void odd()
- {
- char op;//Initialization of variable
- int i, num,odd=0;//Initialization and Declaration of Variables
- printf("\n Enter the size of an array : ");//Initializations and declaration of variables
- scanf("%d", &num);
- int array[num];//Initializing array of same shirt
- printf("\n Enter the elements of the array : ");//Inputtin the size of the array
- for (i = 0; i < num; i++)
- {
- scanf("%d", &array[i]);
- }
- printf("\n Odd numbers in the array are: ");//Displaying the odd elements of the array
- for (i = 0; i < num; i++)
- {
- if (array[i] % 2 != 0)
- {
- odd++;
- printf("%d \t", array[i]);
- }
- }
- }//End of function odd()
- //Defining the function sum_and_avg()
- void sum_and_avg()
- {
- char op;
- int size, i, sum = 0;//Initialization and Declaration of Variables
- printf("\n Enter the size of the arrays : ");//Inputting the size of the array
- scanf("%d", &size);
- int arr[size];//Initializing the array of entered size
- printf("\n Enter the elements of the array :");//Inputting the elements in the array
- //Calculating the Sum of elements of the array
- for (i = 0; i < size; i++)
- {
- scanf("%d", &arr[i]);
- sum += arr[i];
- }
- printf("\n The sum of the array is : %d \n", sum);//Displaying the sum of the elements of the array
- printf("\n The average of the array is : %f", (float)sum / size);//Displaying the average of the elements of the array
- }//End of function sum_and_avg()
- //Defining the function max_min()
- void max_and_min()
- {
- char op;
- int i, max, min, size;//Declaration of Variables
- printf("\n Enter the size of the arrays : ");//Inputting the size of the array
- scanf("%d", &size);
- int arr[size];
- printf("\n Enter the elements of the array : ");//Inputting the elements in the array
- for(i=0; i<size; i++)
- {
- scanf("%d", &arr[i]);
- }
- /* Assume first element as maximum and minimum */
- max = arr[0];
- min = arr[0];
- /* Find maximum and minimum in all array elements*/
- for(i=1; i<size; i++)
- {
- /* If current element is greater than max */
- if(arr[i] > max)
- {
- max = arr[i];
- }
- /* If current element is smaller than min */
- if(arr[i] < min)
- {
- min = arr[i];
- }
- }
- /* Print maximum and minimum element */
- printf("\n Maximum element = %d\n", max);
- printf("\n Minimum element = %d", min);
- }//end of function max_and_min()
- //Defining the function remove_duplicates()
- void remove_duplicates()
- {
- char op;
- int i, j, k, n;//Declaration of variables
- printf("\n Enter array size: ");//Inputting the size of the array
- scanf("%d", &n);
- int arr[n];//Initializing an array of entered size
- printf("\n Enter %d array elements: ", n);//Entering the elements into the array
- for(i = 0; i < n; i++)
- {
- scanf("%d", &arr[i]);
- }
- printf("\n Original array is: ");//Displaying the Original array
- for(i = 0; i < n; i++)
- {
- printf(" %d", arr[i]);
- }
- printf("\n New array is: ");//Displayin the new array after removing duplicate items
- //Calculating the duplicate items and deleting those elements
- for(i = 0; i < n; i++)
- {
- for(j = i+1; j < n; )
- {
- if(arr[j] == arr[i])
- {
- for(k = j; k < n; k++)
- {
- arr[k] = arr[k+1];
- }
- n--;
- }
- else
- {
- j++;
- }
- }
- }
- for(i = 0; i < n; i++)
- {
- printf("%d ", arr[i]);//Displaying elements
- }
- }//end of function remove_duplicates()
- void reverse()
- {
- char op;//Declarations of Variables
- int size, i; //Declarations of Variables
- /* Input size of array */
- printf("\n Enter size of the array : ");
- scanf("%d", &size);
- int arr[size];
- /* Input array elements */
- printf("\n Enter elements in array: ");
- for(i=0; i<size; i++)
- {
- scanf("%d", &arr[i]);
- }
- /*Print array in reversed order*/
- printf("\n Array in reverse order: ");
- for(i = size-1; i>=0; i--)
- {
- printf("%d ", arr[i]);
- }
- }
Output:
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the sum
and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 1
Enter the size of an array : 5
Enter the elements of the
array : 10
11
12
13
14
Even numbers in the array are
: 10 12 14
Do you want to continue
(Y/N)?
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the sum
and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 2
Enter the size of an array : 5
Enter the elements of the
array : 10
12
11
13
14
Odd numbers in the array are:
11 13 Do you want to continue (Y/N)?
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the sum
and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 3
Enter the size of the arrays :
5
Enter the elements of the
array :10
11
12
13
14
The sum of the array is : 60
The average of the array is :
12.000000Do you want to continue (Y/N)?
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the sum
and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 4
Enter the size of the arrays :
5
Enter the elements of the
array : 10
11
12
13
14
Maximum element = 14
Minimum element = 10 Do you
want to continue (Y/N)?
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the sum
and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 5
Enter array size: 5
Enter 5 array elements: 10
11
12
13
14
Original array is: 10 11 12 13 14
New array is: 10 11 12 13 14
Do you want to continue (Y/N)?
1. Print the even-valued elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the
sum and average of the elements of array
4. Print the maximum and minimum
element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 6
Enter size of the array : 5
Enter elements in array: 10
11
12
13
14
Array in reverse order: 14
13 12 11 10 Do you want to continue (Y/N)?
_______________MENU__________
1. Print the even-valued
elements of the array
2. Print the odd-valued
elements of the array
3. Calculate and print the
sum and average of the elements of array
4. Print the maximum and
minimum element of array
5. Remove the duplicates from
the array
6. Print the array in reverse
order
7. Exit
Enter Your Choice : 7
Exiting Program........
May 11, 2020
Array
3 comments
An array is structure.It holds many number in same datatypes.we need to fixed its size first.
Array have two types
1. One dimensional array.
2. Two dimensional array.
- #include<stdio.h>
- #include<conio.h>
- int main()
- {
- //Variable Declaration.
- int a[100],i,n;
- //Take input of max number of array.
- printf("give the number:");
- scanf("%d",&n);
- //Take input in array.
- printf("enter 1 d array digit:\n");
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- //Give output in array.
- printf("output will be:");
- for(i=0;i<n;i++)
- printf("%d\t",a[i]);
- return 0;
- }