Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ.

καλησπερα παιδια ,

 

μας εχουνε βαλει το εξης προβλημα στην σχολη . και μου φαινεται ψιλο δυσκολο . θα ηθελα αν μπορειται να με βοηθησετε . δειτε το λοιπον .

 

 

Lab Assignment 2.1:

Write a main program which reads a sequence of four real numbers (data type float) on one line

from the keyboard. The parameters (four per line) are

1. (current) temperature in degrees Celsius,

2. (current) direction of wind in degrees (0 degree represents „NORTH“),

3. (current) wind speed in km/h

4. (current) wind chill in km/h

The end of the sequence from the keyboard is recognized by the first character which is not a

number (e.g. a letter). You don’t have to (and must not …) use an array for the values

entered from the keyboard because the number of values is not known in advance. For the

numbers entered, the program must calculate and display after each new set of values (see 1-4):

1 how many real numbers were read,

2 the maxima of temperature, wind speed and wind chill

3 the minima of temperature, wind speed and wind chill

4 the mean value of temperature, wind speed, wind chill and wind direction

5 the mean square deviation (standard deviation) of temperature, wind speed, wind chill

and wind direction

 

απο οτι εχω καταλαβει πρεπει να φτιαξω ενα προγραμμα που να βαζεις εσυ 4 τιμες και μετα αυτο να κανει λουπα καθε φορα και μετα απο καθε λουπα να συκρινει τις τιμες . τις μεγαλυτερεσ και τις μικροτερες . και οταν δινω εναν γραμμα η ακολουθεια πρεπει να σταματαει. αλλα δεν ξερω πρωτων αν ειμαι σωστος και δευετρος να το υλοποιησω. αν μπορειται βοηθιστε

Δημοσ.

ναι και κάθε φορά προσθέτεις στις μεταβλητές και χρησιμοποιείς ένα loop counter για να βρεις μεσες τιμές κτλ

... είναι πολύ εύκολο ... ξεκίνα το και αμα κολλήσεις ρώτα ...

Δημοσ.

Εγώ πάλι δεν το βρίσκω πολύ εύκολο.

 

Σου έγραψα πρόχειρο κώδικα για total, min και max που δεν είναι efficient, αλλά δείχνει να δουλεύει (είναι και περασμένη η ώρα). Επίσης δεν είμαι σίγουρος πως έχω ερμηνεύσει σωστά την εκφώνηση...

 

 

 

>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <math.h>
#include <stdbool.h>	// C99

#define MAXINPUT		(255+1)
#define MAX_LNTOKENS		4

#define FLTZERO			(0.00001)
#define FLT_ISZERO( f )		( fabsf( (f) ) < FLTZERO )
#define FLT_ISEQUAL( f1, f2 )	( FLT_ISZERO( ((f1)-(f2)) ) )


/**************************************************//**
* @par Prototype:
* int s_tokenize( char *s, char *tokens[], const int ntoks, const char *delims )
*
* @brief	Break a string up to ntoks tokens and store them in *tokens[]
* 		(uses any char in delims as separator between tokens)
* @return 	The number of tokens, or 0 on failure.
******************************************************
*/
int s_tokenize( char *s, char *tokens[], int ntoks, const char *delims )
{
int i=0;

/* sanity checks */
if ( !s || !tokens || !delims ) {
	return 0;
}
if ( !*s || !*delims || ntoks < 1 ) {
	return 0;
}

tokens[ 0 ] = strtok(s, delims);
if ( NULL == tokens[0] )
	return 0;
for (i=1; i < ntoks && (tokens[i]=strtok(NULL, delims)) != NULL; i++)
	;	/* void */

return i;
}

/**************************************************//**
*
******************************************************
*/
bool s_is_float( const char *s )
{
if ( !s || !*s )
	return false;

for (int i=0; s[i]; i++)
{
	if ( !isdigit(s[i]) ) {
		if ( '.' == s[i] || (0 == i && '-' == s[i]) )
			continue;
		return false;
	}
}

return true;

}

/**************************************************//**
*
******************************************************
*/
void arrFloat_init( float arrFloat[], int maxelems, float val )
{
if ( !arrFloat )
	return;

for (int i=0; i < maxelems; i++)
	arrFloat[i] = val;
}

/**************************************************//**
*
******************************************************
*/
void arrFloat_print_tabular(
char 		**colLabels,
const char 	*rowLabel,
float 		arrFloat[],
float 		emptyVal
)
{
const int fmtWidth = 12;

if ( !arrFloat )
	return;

if ( colLabels ) {
	printf( "\t\t\t" );
	for (; *colLabels; colLabels++)
		printf( "%-*s", fmtWidth, *colLabels );
	putchar('\n');
}

if ( rowLabel )
	printf( "\t%-*s", fmtWidth, rowLabel );
else
	printf( "\t\t" );

// temp
if ( FLT_ISEQUAL(emptyVal, arrFloat[0]) )
	printf( "%-*c", fmtWidth, '-' );
else
	printf( "\t%-*.*f", fmtWidth,3, arrFloat[0] );

// wind speed
if ( FLT_ISEQUAL(emptyVal, arrFloat[2]) )
	printf( "%-*c", fmtWidth, '-' );
else
	printf( "%-*.*f", fmtWidth,3, arrFloat[2] );

// wind chill
if ( FLT_ISEQUAL(emptyVal, arrFloat[3]) )
	printf( "%-*c\n", fmtWidth, '-' );
else
	printf( "%-*.*f\n", fmtWidth,3, arrFloat[3] );

fflush( stdout );
}

/**************************************************//**
*
******************************************************
*/
int main( void )
{
char	input[MAXINPUT]			= {'\0'};
int	ntoks				= 0;
char	*tokens[MAX_LNTOKENS];
float	arrFloats[MAX_LNTOKENS]		= {0.0f};
float	arrFloatMaxes[MAX_LNTOKENS]	= {0.0f};
float	arrFloatMins[MAX_LNTOKENS]	= {0.0f};
int	nFloats				= 0;
bool	stop				= false;
bool	printStats			= true;
int 	i				= 0;
char	*colLabels[]			= {"temp", "wind", "chill", NULL};

arrFloat_init( arrFloatMaxes, MAX_LNTOKENS, -FLT_MAX );
arrFloat_init( arrFloatMins, MAX_LNTOKENS, FLT_MAX );

while ( !stop )
{
	fgets(input, MAXINPUT, stdin);
	ntoks = s_tokenize(input, tokens, MAX_LNTOKENS, " \t\n");
	memset( arrFloats, 0, sizeof(arrFloats) );
	for (i=0; i < ntoks; i++)
	{
		if ( !s_is_float(tokens[i]) ) {
			stop = true;
			if (0 == i)
				printStats = false;
			i = ntoks;
			break;
		}
		arrFloats[i] = atof(tokens[i]);
		nFloats++;
		if ( arrFloats[i] > arrFloatMaxes[i] )
			arrFloatMaxes[i] = arrFloats[i];
		if ( arrFloats[i] < arrFloatMins[i] )
			arrFloatMins[i] = arrFloats[i];

	}

	if ( printStats && i == ntoks ) {
		printf( "\tTotal numbers read: %d\n", nFloats );
		arrFloat_print_tabular(colLabels, "Max values:", arrFloatMaxes, -FLT_MAX);
		arrFloat_print_tabular(NULL, "Min values:", arrFloatMins, FLT_MAX);
	}
}

system("pause");	// windows only
exit(0);
}

 

 

Δημοσ.

για μέσες τιμές

 

>
accumulator+=incoming
current_mean_0=accumulator/counter
counter++

 

εαν πω και άλλα θα το έχουμε λύσει όλο ...

mingf1, αν και δεν έκανα compile τον κωδικά σου ... όντως τώρα δεν είναι πολύ εύκολο ...

του το κάναμε πανεύκολο :-)

Δημοσ.

:lol:

 

Βασικά έχει θέμα με τις προδιαγραφές της εκφώνησης για το input. Εκεί ήταν που δεν το βρήκα εύκολο. Τα υπόλοιπα είναι όντως εύκολα.

 

Δημοσ.

:lol:

 

Βασικά έχει θέμα με τις προδιαγραφές της εκφώνησης για το input. Εκεί ήταν που δεν το βρήκα εύκολο. Τα υπόλοιπα είναι όντως εύκολα.

 

θα πρέπει να ιδρώσει λίγο το wind direction ... αλλά θα τους έχει πει κάτι στην τάξη ...

Δημοσ.

ευχαριστω παιδια , εγω προσπαθησα να το κανω με functions αλλα δεν δουλευει και δεν μπορω να βρω το γιατι. να σας κανω copy/ paste τον κωδικα? αμα δεν το βρω θα παω για while μαλλον .

 

οριστε ο κωδικας :

 

 

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

float fmax (float);

float fmin (float);

float fmean (float);

float fmean_sqr_dev (float);

//float temp, speed, direction, chill, input_value;

int main()

{

float temp, speed, direction, chill;

printf (" Enter the values for Temperature, Wind Speed, Wind Direction, Wind Chill \n");

scanf ("%f %f %f %f", &temp, &speed, &direction, &chill);

printf ("The maximum temperatute is %f\n", &fmax);

system ("pause");

return 0;

 

}

float fmax (float input_value, float max_value, float temp)

{

input_value = temp;

max_value == '0';

if ( input_value > max_value)

{max_value = input_value;}

printf("%f \n", &max_value);

}

float fmin (float input_value, float min_value)

{

min_value == 0;

if ( input_value > min_value) return min_value = input_value;

else return min_value = min_value;

printf("%f \n", min_value);

}

float fmean (float input_value, float sum, float count, float mean)

{

sum == 0;

count == 0;

input_value + sum == sum;

count + 1 == count;

sum/count == mean;

printf("%f \n", mean);

}

float fmean_sqr_dev (float input_value, float sum, float count, float mean, float mean_sqr_dev)

{

sum == 0;

count == 0;

input_value + sum == sum;

count + 1 == count;

sum * sum == sum;

sum - (count * pow(input_value, -2)) == sum;

pow (sum, 1/2) == mean_sqr_dev;

printf("%f \n", mean_sqr_dev);

}

 

μας ειπαι να μην το κανουμε με arreys

Δημοσ.

Βάλε αν θες τον κώδικά σου μέσα σε code-tags (δλδ

>, χωρίς κενά στις αγκύλες) γιατί έτσι δεν διαβάζεται.

Επίσης, διόρθωσε όλα εκείνα τα == που έχεις στον κώδικα, τα οποία πιθανότατα τα εννοούσες ως =.

Σχετικά με τα arrays, το διάβασα κι εγώ στην εκφώνηση αλλά ως αιτιολογία της μη χρήσης τους δίνει το ότι δεν ξέρουμε εκ των προτέρων πόσα νούμερα θα μας δώσει ο χρήστης σε κάθε γραμμή.

Στον κώδικα που σου έδωσα, που έχει array, ο χρήστης μπορεί να δώσει από κανένα έως όσα νούμερα θέλει στην κάθε γραμμή (και 10 και 20) και ο κώδικας λειτουργεί σωστά.

Ακόμα κι αν σου δώσει κάτι σαν κι αυτό...

[code]
34.4 d

 

Ακόμα κι αν σου πατήσει σκέτο ENTER χωρίς να έχει γράψει τίποτα.

 

Οπότε, θεώρησα πως καλύπτει την εκφώνηση. Από την άλλη μεριά, όπως έγραψα και στο αρχικό μου ποστ, δεν είμαι σίγουρος πως έχω ερμηνεύσει σωστά την εκφώνηση.

 

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...