Write a C program to read N temperature readings as an array and search an element in array using pointers. Read the array and pass the array to the function "temperature()"Sample Input316.8 10.1 2.32.3Sample Outputfound at position 3
Question
Write a C program to read N temperature readings as an array and search an element in array using pointers. Read the array and pass the array to the function "temperature()"Sample Input316.8 10.1 2.32.3Sample Outputfound at position 3
Solution 1
Sure, here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
printf("Enter the number of temperature readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the temperature readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
float val;
printf("Enter the temperature value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
This program first reads the number of temperature readings from the user. Then it reads the temperature readings and stores them in an array. It then reads the temperature value to search from the user. It then calls the temperature function passing the array, the number of elements in the array, and the value to search. The temperature function then iterates over the array using a pointer and if it finds the value, it prints the position of the value in the array and returns. If it doesn't find the value, it prints "Not found".
Solution 2
Here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
float val;
printf("Enter the value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
In this program, we first read the number of temperature readings from the user. Then we read the temperature readings and store them in an array. We then ask the user for a value to search in the array. We pass the array, the number of elements in the array, and the value to search to the temperature() function. In the temperature() function, we iterate over the array using a pointer and if we find the value, we print the position and return. If we don't find the value, we print "Not found".
Solution 3
Here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
float val;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
printf("Enter the value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
In this program, we first read the number of temperature readings from the user. Then we read the temperature readings and store them in an array. We then ask the user for the value to search in the array. We pass the array, the number of elements in the array, and the value to search to the function temperature(). In the temperature() function, we iterate over the array using a pointer and if we find the value, we print the position and return from the function. If we don't find the value, we print "Not found".
Solution 4
Here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0
Solution 5
Sure, here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
float val;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
printf("Enter the value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
This program works as follows:
- It first reads the number of temperature readings from the user.
- Then it reads the temperature readings and stores them in an array.
- It asks the user for a value to search in the array.
- It passes the array, the number of elements and the value to search to the function
temperature(). - The function
temperature()iterates over the array using a pointer and checks if the current element is equal to the value to search. If it is, it prints the position of the element (1-indexed) and returns. If it doesn't find the value, it prints "Not found".
Solution 6
Here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
float val;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
printf("Enter the value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
In this program, we first read the number of temperature readings from the user. Then we read the temperature readings and store them in an array. We then ask the user for the value to search in the array. We pass the array, the number of elements in the array, and the value to search to the function temperature(). In the temperature() function, we iterate over the array using a pointer and if we find the value, we print the position and return from the function. If we don't find the value, we print "Not found".
Solution 7
Sure, here is a simple C program that reads N temperature readings as an array and searches an element in the array using pointers.
#include <stdio.h>
void temperature(float *arr, int n, float val) {
for(int i = 0; i < n; i++) {
if(*(arr + i) == val) {
printf("found at position %d\n", i + 1);
return;
}
}
printf("Not found\n");
}
int main() {
int n;
float val;
printf("Enter the number of readings: ");
scanf("%d", &n);
float arr[n];
printf("Enter the readings: ");
for(int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
printf("Enter the value to search: ");
scanf("%f", &val);
temperature(arr, n, val);
return 0;
}
In this program, we first read the number of temperature readings (n) and the readings themselves into an array. We then read the value to search for. We pass the array, the number of readings, and the value to search for to the temperature() function. This function goes through the array using a pointer and checks if each element is equal to the value we're searching for. If it finds the value, it prints the position and returns. If it doesn't find the value, it prints "Not found".
Similar Questions
Create an array to store temperatures. Read N temperatures recorded in a day. Create another array to represent the updated temperatures. Copy the elements of the temperature array into the updated temperature array and increment the elements of the temperature array by a value of 10 if the temperature is >30.Print the updated temperature.Print the temperature.Sample Input4 (Read N -Total number of temperatures recorded)30 323723Sample Output30 42 47 2330 32 37 23
Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c
Write C program to insert the values at the specific index in the array.
Write a program in C to insert New value in the array at a given positionNote: First read array size then elements and then position and its valueFor example:Test Input Result151 2 3 4 53 8New Array is 1 2 8 3 4 5
Daily Temperature AnalysisYou are a meteorologist who needs to analyze temperature data for the last 10 days. Write a program that reads the temperatures into an array and finds the average temperature over these 10 days.Constraints:NAExample:Sample Input:30323129283335343231Sample Output:31.5Explanation:The average temperature over 10 days is 31.5
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.