यहाँ यह कोड यादृच्छिक संख्याओं को हथियाने के लिए है(तापमान) और उसी घंटे के साथ एक तालिका बनाएं जिसमें तापमान दर्ज किया गया था। यहाँ आउटपुट का एक उदाहरण है जो मुझे प्राप्त होने वाला है।
Temperature Conditions on October 9, 2015:
Time of Day Temperature in degrees F
0 85
1 80
2 97
3 90
4 68
5 75
6 77
7 98
8 97
9 62
etc...
Maximum Temperature for the day: <whatever> Degrees F
Minimum Temperature for the day: <whatever> Degrees F
Average Temperature for the day: <whatever.whatever> Degrees F
मेरी समस्या यह है कि जब मैं कोड चलाता हूं तो एक डायलॉग बॉक्स दिखाई देता है और कहता है कि कार्यक्रम ने काम करना बंद कर दिया है और मुझे यह पता नहीं है कि मैं क्यों नहीं जानता।
सभी मदद बहुत सराहना की जाएगी।
#include <stdio.h>
#include <stdlib.h>
int GetValue(int[]);
int main() {
int x, n, max = 0,min = 100, ArrayNumMax, ArrayNumMin, temperature[25];
float sum;
float average;
int num[25];
printf("Temperature Conditions on October 9, 2015:nTime of Day tTemperature in Degrees Fn");
for (x = 0; x <= 24; x++) {
//if statements to get min and max
temperature[x] = GetValue(temperature);
if (temperature[x] > max) {
max = temperature[x];
ArrayNumMax = x;
}
if (temperature[x] < min) {
min = temperature[x];
ArrayNumMin = x;
}
printf("t%dttttt%dn", x,temperature[x]);
}
//prints statements
printf("nMidnighttttt%dnnMaximum Temperature for the day: %d Degrees F at %dnMinimum Temperature for the day: %d Degrees F at %dn", temperature[12],max,ArrayNumMax, min, ArrayNumMin);
//adds up all temps
sum=0;
for (x=0;x<25;x++){
sum=(sum+temperature[x]);
}
//prints and creates average
average=sum/25;
printf("Average Temperature for the day: %.2f Degrees Fn",average);
return 0;
}
//gets values and puts them into array
int GetValue(int value[]) {
int x, temp[x];
temp[x] = (rand()%(100-60+1))+60;
return temp[x];
}
उत्तर:
उत्तर № 1 के लिए 11
आप अपने में क्या कर रहे हैं? getValue समारोह?
int GetValue(int value[]) {
int x, temp[x]; // create an array of undeclared amount x....
temp[x] = (rand()%(100-60+1))+60; // at invalid memory set it to this value.
return temp[x]; // then return this memory that I have no control over
}
इसे स्क्रैप करें और इसके साथ जाएं ...
void GetValue(int value[], int x) {
value[x] = (rand()%(100-60+1))+60;
}
मुख्य बदलाव के ऊपर भी
int GetValue(int[]);
सेवा मेरे
void GetValue(int a[], int b);
फिर अपने मेन में
//if statements to get min and max
GetValue(temperature, x);
if (temperature[x] > max) {
इसके अलावा आपको प्रीप्रोसेसर मैक्रोज़ में देखना चाहिए। उनके बारे में यहां पढ़ें। http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm
जैसे #define
#define array_size 25
int array[array_size];
फिर यदि आप अपना कोड बदलते हैं और 25 के बजाय आपको 50 की आवश्यकता है तो बस इसे एक बार बदल दें।
उत्तर № 2 के लिए 1
दो त्रुटि:
x
मेंGetValue
आरंभिक नहीं हैtemp[x]
सीमा पार कर गया है, इसलिए कार्यक्रम एक विभाजन दोष का सामना करता है।- जब आप परिभाषित करते हैं
temp[x]
,x
"मान अपरिभाषित है, औरtemp
कार्यक्रम के ढेर से अधिक है।
सही इस तरह से हो सकता है:
int GetValue(int value[])
{
int x;
x=1;
int temp[x];
temp[x-1] = (int)((rand()%(100-60+1))+60);
return temp[x-1];
}
परिणाम से पता चलता है कि:
Temperature Conditions on October 9, 2015:
Time of Day Temperature in Degrees F
0 65
1 96
2 60
3 83
4 67
5 79
6 66
7 92
8 83
9 77
10 87
11 66
12 77
13 66
14 68
15 82
16 74
17 79
18 63
19 73
20 86
21 70
22 80
23 81
24 80
Midnight 77
Maximum Temperature for the day: 96 Degrees F at 1
Minimum Temperature for the day: 60 Degrees F at 2
Average Temperature for the day: 76.00 Degrees F