c program

linear search program*

// Take an array as input from the user. Search for a given number x and print the index at where x is present in the array.

#include <stdio.h>

int main(void) {
  int n,x;

  printf("Enter the positive size of the array: ");
  scanf("%d",&n);
 
  int arr[n];
  printf("Enter the element of array: ");
  for(int i=0; i<n; i++){
  scanf("%d", &arr[i]);
  }
 
  printf("Enter which number you want to search: ");
  scanf("%d",&x);
 
   int found = 0;
  for(int i = 0; i<n; i++){
    if(arr[i]==x){
      printf("The index is %d and the the number is %d",i, x);
      found = 1;
      break;
    }
    if(!found) {
      printf("The index is not present");
    }
  }
  return 0;

 

// Taking an array as an input and printing its elements.
#include <stdio.h>

int main() {
int n;

// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare the array with the specified size
int arr[n];

// Get the elements of the array from the user
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Print the elements of the array
printf("The elements of the array are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
 
 
 
 
//to know how calculate the length of array read line number 5



- This line is a preprocessor directive that instructs the compiler to include the contents of the header file `stdio.h`.
- This header file contains declarations for standard input/output functions (e.g., `printf`, `scanf`) used for interacting with the console/terminal.
- Without this line, the program would lack the necessary definitions to use these functions.

**Line 3: `int main() {`**

- This line declares the `main` function, which is the entry point of execution for a C program.
- The `int` return type indicates that the function will return an integer value (typically 0 to signal successful execution).
- The empty parentheses `()` signify that the `main` function takes no arguments from the command line.

**Line 4: `int myNumbers[] = {25, 50, 75, 100};`**

- This line declares an integer array named `myNumbers` and initializes it with the values `25`, `50`, `75`, and `100`.
- The empty square brackets `[]` indicate that this is an array.
- Arrays store multiple elements of the same data type (in this case, integers).
- The initializer list `{25, 50, 75, 100}` assigns these values to the elements of the array.

**Line 5: `int length = sizeof(myNumbers) / sizeof(myNumbers[0]);`**

- This line calculates the length of the `myNumbers` array and stores it in the `length` variable.
- `sizeof(myNumbers)` gives the total size in bytes of the entire array (4 elements * 4 bytes/element = 16 bytes).
- `sizeof(myNumbers[0])` gives the size of a single element (1 integer * 4 bytes/integer = 4 bytes).
- Dividing the former by the latter gives the number of elements, which is the array's length (16 bytes / 4 bytes/element = 4 elements).

**Line 7: `for (i = 0; i < length; i++) {`**

- This line starts a `for` loop that iterates 4 times (once for each element in the array).
- `i` is the loop counter initialized to 0 (the first index of the array).
- The loop continues as long as `i` is less than `length` (0, 1, 2, and 3, covering all indices).
- At the end of each iteration, `i` is incremented by 1 to move to the next element.

**Line 8: `printf("%d\n", myNumbers[i]);`**

- This line prints the value of the current element (`myNumbers[i]`) to the console/terminal.
- `printf` is a formatted output function from `stdio.h`.
- `"%d"` is a format specifier that tells `printf` to expect an integer argument.
- `\n` inserts a newline character after printing, moving the cursor to the next line.

**Line 10: `return 0;`**

- This line returns the value 0 from the `main` function, indicating successful program execution.
- By convention, returning 0 from `main` signals a smooth exit to the operating system.

**Summary:**

The code effectively prints the values of the elements in the `myNumbers` array (25, 50, 75, and 100) to the console, one value per line.

I hope this explanation is comprehensive and helpful! 

 

 

class Book{ 

//Properties, Fields, Global Variables
str title ;
int pageCount ;
int ISBN ;
Boolean isCheckedout ; //whether or not the book is cheeked out.
int dayCheckedOut = -1;


//Constructor
Book(string bookTitle, int bookPageCount, int bookISBN) {
this. Title =bookTitle;
this.pageCount = bookPageCount;
this.ISBN = bookISBN;
}

//Getters
getTitle() {

  }
}
 
 
 
  1. Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.

eg:

 

 

 


 

 

 

Comments

Popular posts from this blog

CyberSecurity

VERTICAL SCALING 💋

prisma using in project idx 13.3