Design a C function to print the elements of a given integers array.

26 Sep 2022 Balmiki Mandal 0 C Programming

Print Elements of an Integer Array: Function Design in c

Designing a function to print the elements of a given integer array means creating a function that takes an integer array and its size as parameters, and outputs the elements of the array to the console.

To design a function to print the elements of a given integers array in C, we can follow these steps:

Declare the function prototype. 

This specifies the function name, return type, and parameter types.

C Programming
void print_array(int arr[], int size);

Implement the function body. 

This is where we write the code to print the elements of the array. One way to do this is to use a for loop to iterate over the array and print each element using the printf() function.

C Programming
void print_array(int arr[], int size) {
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");
}

Call the function from the main() function. 

To print the elements of an array, we can pass the array and its size as arguments to the print_array() function.

C Programming
int main() {
  int arr[] = {1, 2, 3, 4, 5};
  int size = sizeof(arr) / sizeof(arr[0]);

  print_array(arr, size);

  return 0;
}

Output:

1 2 3 4 5

We can also design a more generic function to print the elements of an array of any data type. To do this, we can use a template.

C Programming
template <typename T>
void print_array(T arr[], int size) {
  for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");
}

To use this function, we can simply pass the array and its size as arguments, and the compiler will automatically infer the data type of the array elements.

For example, to print the elements of an array of floats, we can do the following:

C Programming
float arr[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
int size = sizeof(arr) / sizeof(arr[0]);

print_array(arr, size);

Output:

 

1.000000 2.000000 3.000000 4.000000 5.000000

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.