Skip to main content

🧪 Practice Problems Answer Day 2 DSA with C

 



✅ 1. Find Maximum and Minimum in an Array


#include <stdio.h> int main() { int n, i; int arr[100], max, min; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } max = min = arr[0]; for (i = 1; i < n; i++) { if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i]; } printf("Maximum: %d\n", max); printf("Minimum: %d\n", min); return 0; }

✅ 2. Count Occurrences of a Given Number


#include <stdio.h> int main() { int n, x, count = 0; int arr[100]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter number to count: "); scanf("%d", &x); for (int i = 0; i < n; i++) { if (arr[i] == x) count++; } printf("Number %d appears %d time(s).\n", x, count); return 0; }

✅ 3. Reverse an Array


#include <stdio.h> int main() { int n, arr[100]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Reversed array:\n"); for (int i = n - 1; i >= 0; i--) { printf("%d ", arr[i]); } return 0; }

📘 Homework


✅ 4. Copy One Array to Another


#include <stdio.h> int main() { int n, arr1[100], arr2[100]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements for arr1:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr1[i]); arr2[i] = arr1[i]; // Copying } printf("Copied array (arr2):\n"); for (int i = 0; i < n; i++) { printf("%d ", arr2[i]); } return 0; }

✅ 5. Merge Two Arrays


#include <stdio.h> int main() { int arr1[50], arr2[50], merged[100]; int n1, n2, i; printf("Enter number of elements in arr1: "); scanf("%d", &n1); printf("Enter %d elements for arr1:\n", n1); for (i = 0; i < n1; i++) scanf("%d", &arr1[i]); printf("Enter number of elements in arr2: "); scanf("%d", &n2); printf("Enter %d elements for arr2:\n", n2); for (i = 0; i < n2; i++) scanf("%d", &arr2[i]); for (i = 0; i < n1; i++) merged[i] = arr1[i]; for (i = 0; i < n2; i++) merged[n1 + i] = arr2[i]; printf("Merged array:\n"); for (i = 0; i < n1 + n2; i++) printf("%d ", merged[i]); return 0; }

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...