Write a c language average function one without passing argument and return and one with…
- User
Write a c language average function one without passing argument and return and one with passing argument and return
- Assistant
Here is an example of a C language average function without passing arguments and return:
c#include <stdio.h> void average() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; int sum = 0; printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } float avg = (float)sum / n; printf("Average: %.2f\n", avg); } int main() { average(); return 0; }And here is an example of a C language average function with passing argument and return:
c#include <stdio.h> float average(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } return (float)sum / n; } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); float avg = average(arr, n); printf("Average: %.2f\n", avg); return 0; } - User
I mean average of sum
- Assistant
I apologize for the confusion. If you want to calculate the average of the sum of an array, here are the modified examples:
Example of a C language average function without passing arguments and return for the average of sum:
c#include <stdio.h> void average() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; int sum = 0; printf("Enter %d elements:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } float avg = (float)sum / n; printf("Average of sum: %.2f\n", avg); } int main() { average(); return 0; }Example of a C language average function with passing arguments and return for the average of sum:
c - User
Average of 3 number
- Assistant
Here is an example of a C program to calculate the average of three numbers:
c#include <stdio.h> float average(float num1, float num2, float num3) { return (num1 + num2 + num3) / 3; } int main() { float num1, num2, num3; printf("Enter three numbers: "); scanf("%f %f %f", &num1, &num2, &num3); float avg = average(num1, num2, num3); printf("Average: %.2f\n", avg); return 0; }In this program, we define a function named
averagethat takes three floating-point numbers as arguments and returns their average. In themainfunction, we prompt the user to enter three numbers, read them usingscanf, and calculate the average using theaveragefunction. Finally, the average is printed to the console. - User
Sawp number function in c without using argument and return
- Assistant
Here is an example of a C program to swap two numbers without using arguments and return:
c#include <stdio.h> void swap() { int num1, num2, temp; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2); temp = num1; num1 = num2; num2 = temp; printf("After swapping: num1 = %d, num2 = %d\n", num1, num2); } int main() { swap(); return 0; }In this program, we define a function named
swapthat doesn't take any arguments. Inside the function, we prompt the user to enter two numbers, read them usingscanf, and then perform the swapping operation using a temporary variable. Finally, we print the values of the two numbers before and after swapping. In themainfunction, we simply call theswapfunction to execute the swapping logic.