I have to do a program that determine the biggest
number in the month
I have to use enum
so I was wondering can I declare an array of a type
like
#include <stdio.h>
int main()
{
int h_rain_fall, n_months, i;
enum months {jan=23, feb=19, apr=27, mar=38, may=33, jun=24, jul=20,
aug=18, sep=26, oct=30, nov=26, dec=22};
string a_months[jan,feb,apr,mar,may,jun,jul,aug,sep,oct,nov,dec];
n_months = 12;
i = 0;
for(i=n_months; i!=0 ; i--) {
printf("teste %d", a_months[i]);
if (a_months[i] > h_rain_fall)
{
h_rain_fall = a_months[i];
}
else
{
//skip
}
}
printf("The highst rain fall is %d",h_rain_fall);
return(0);
}
Thanks Profetas 9 1314
Profetas <xu*****@yahoo.com> scribbled the following: I have to do a program that determine the biggest number in the month
I have to use enum so I was wondering can I declare an array of a type like
#include <stdio.h>
int main() { int h_rain_fall, n_months, i; enum months {jan=23, feb=19, apr=27, mar=38, may=33, jun=24, jul=20, aug=18, sep=26, oct=30, nov=26, dec=22}; string a_months[jan,feb,apr,mar,may,jun,jul,aug,sep,oct,nov,dec];
That won't work. It will declare an array of dec, i.e. 22, strings.
You want:
int a_months[12] = {jan, feb, apr, mar, may, jun, jul, aug, sep,
oct, nov, dec};
"string" is not a standard C type, possibly you mean int.
You possibly may wish to have March before April.
n_months = 12; i = 0;
for(i=n_months; i!=0 ; i--) {
C arrays begin from 0, so you want to have:
for(i=n_months-1; i>=0; i--) {
here.
But why are you iterating the array in reverse order anyway?
printf("teste %d", a_months[i]); if (a_months[i] > h_rain_fall) { h_rain_fall = a_months[i]; } else { //skip } } printf("The highst rain fall is %d",h_rain_fall); return(0); }
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Life without ostriches is like coffee with milk."
- Mika P. Nieminen
Profetas wrote: I have to do a program that determine the biggest number in the month
I have to use enum so I was wondering can I declare an array of a type like
#include <stdio.h>
int main() { int h_rain_fall, n_months, i; enum months {jan=23, feb=19, apr=27, mar=38, may=33, jun=24, jul=20, aug=18, sep=26, oct=30, nov=26, dec=22}; string a_months[jan,feb,apr,mar,may,jun,jul,aug,sep,oct,nov,dec];
You probably want
int a_months = {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}; n_months = 12; i = 0;
for(i=n_months; i!=0 ; i--) {
You never initialized h_rain_fall. You can do that with the
for loop. See below. printf("teste %d", a_months[i]); if (a_months[i] > h_rain_fall) { h_rain_fall = a_months[i]; } else
As is, you don't need the else clause. { //skip } } printf("The highst rain fall is %d",h_rain_fall);
Put the newline character, '\n', in the printf statements.
return(0); }
#include <stdio.h>
int main(void)
{
int h_rain_fall, n_months, i;
enum months {jan=23, feb=19, mar=38, apr=27, may=33,
jun=24, jul=20, aug=18, sep=26, oct=30,
nov=26, dec=22};
int a_months[] = {jan,feb,mar,apr,may,jun,jul,aug,sep,
oct,nov,dec};
for(i=h_rain_fall=0,n_months=12; i < n_months ; i++)
{
printf("teste %d\n", a_months[i]);
if (a_months[i] > h_rain_fall)
h_rain_fall = a_months[i];
}
printf("The highst rain fall is %d\n",h_rain_fall);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
Al Bowers <xa******@rapidsys.com> writes: You probably want
int a_months = {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
I think you probably want an array there.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Joona I Palaste <pa*****@cc.helsinki.fi> wrote: Profetas <xu*****@yahoo.com> scribbled the following: I have to do a program that determine the biggest number in the month
I have to use enum
#include <stdio.h>
int main() { int h_rain_fall, n_months, i; enum months {jan=23, feb=19, apr=27, mar=38, may=33, jun=24, jul=20, aug=18, sep=26, oct=30, nov=26, dec=22}; string a_months[jan,feb,apr,mar,may,jun,jul,aug,sep,oct,nov,dec];
That won't work. It will declare an array of dec, i.e. 22, strings. You want: int a_months[12] = {jan, feb, apr, mar, may, jun, jul, aug, sep, oct, nov, dec}; "string" is not a standard C type, possibly you mean int.
I'd say, for an array of 12 enum months, give it type enum months...
enum months a_months[12]={jan, feb, mar, apr, /* etcetera */ dec}; n_months = 12;
Even better to make this a #defined constant, since it doesn't change
and using it throughout the program will make it clearer which 12 means
"number of months" and which 12 means, e.g., "column width which happens
to be 12 as well".
So at the start of the program (in my style, usually just after any
#includes, but tastes differ):
#define N_MONTHS 12
and then you can do
enum months a_months[N_MONTHS]={jan, feb, mar, /* etcetera */ dec};
as well as
for (i=0; i<N_MONTHS; i++)
Not only is this clearer, if you ever move to a country with 13 months
(hey, we might start living on other planets one day!), you only need to
change one number, add one enum constant in two places, and recompile.
You can do similar things using an extra item in your enum months, btw,
if you feel that is better style.
Richard
#include <stdio.h>
int main()
{
int h_rain_fall, n_months, i;
enum months {jan=23, feb=19,mar=38, apr=27, may=33, jun=24, jul=20,
aug=18, sep=26, oct=30, nov=26, dec=22};
int a_months[12] = {jan, feb, apr, mar, may, jun, jul, aug, sep, oct,
nov, dec};
n_months = 12;
i = 0;
h_rain_fall = 0;
for(i=0; i!=n_months ; i++) {
printf("teste %d\n", a_months[i]);
if (a_months[i] > h_rain_fall)
{
h_rain_fall = a_months[i];
}
else
{
//skip
}
}
printf("teste %d",jan);
printf("The highst rain fall is %d \n",h_rain_fall);
return(0);
}
How will I ref back to the name? I can find the highest number, i.g. will
be 38. how can I print 38 mar?
Is there any way to create an array of arrayc of char?
like int a_months[12] =
{a[4]="jan", b[4]="feb", c[4]="apr" ....};
thanks
Profetas wrote: #include <stdio.h>
int main() { int h_rain_fall, n_months, i; enum months {jan=23, feb=19,mar=38, apr=27, may=33, jun=24, jul=20, aug=18, sep=26, oct=30, nov=26, dec=22}; int a_months[12] = {jan, feb, apr, mar, may, jun, jul, aug, sep, oct, nov, dec};
n_months = 12; i = 0; h_rain_fall = 0;
for(i=0; i!=n_months ; i++) {
for(i = 0; i < n_months, i++)
printf("teste %d\n", a_months[i]); if (a_months[i] > h_rain_fall) { h_rain_fall = a_months[i]; } else { //skip } } printf("teste %d",jan);
Better to make it:
printf("teste %d\n",jan);
printf("The highst rain fall is %d \n",h_rain_fall); return(0); }
How will I ref back to the name? I can find the highest number, i.g. will be 38. how can I print 38 mar?
Is there any way to create an array of arrayc of char? like int a_months[12] = {a[4]="jan", b[4]="feb", c[4]="apr" ....};
thanks
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
The specification is soo poor, I don't know If I have to
print the month name, I do have to use enum, that is all
wrong, what do you think about the specification?
I can't really get what he is after.
3 Using emacs, compose an algorithm (expressed in pseudocode) for finding
the month with maximum rainfall from the table below.
23 19 27 38 33 24 20 18 26 30 26 22
Begin as before by establishing the context and then proceed to design
your algorithm via two (or more) levels of pseudocode. Again, stay within
the style conventions for pseudocode. highest
[12 marks]
4 Enclose your solution to Exercise 3 in a comment block and then
implement your algorithm in C, adding comments to your code. As before,
stepwise refine your implemen-tation and define no function other than
main.
Use enumeration to define any new data type. Recall that C affords the
ability to define a value within (constant of) an array type. Monthly
rainfall may be established in this way, as can the usual three-letter
abbreviations of month names.
That is my solution
#include <stdio.h>
int main()
{
typedef char M_NAME[4];
int h_rain_fall, n_months, y, position;
enum months {jan=23, feb=19,mar=38, apr=27, may=33, jun=24, jul=20,
aug=18, sep=26, oct=30, nov=26, dec=22};
int a_months[12] = {jan, feb, apr, mar, may, jun, jul, aug, sep, oct,
nov, dec};
M_NAME a = "jan" , b = "feb" , c = "mar", d = "apr" , e = "may", f =
"jun", g = "jul", h = "aug", i = "sep", j = "oct" , l = "nov", m = "dec"
;
n_months = 12;
y = 0;
position = 0;
h_rain_fall = 0;
for(y=0; y!=n_months ; y++) {
if (a_months[y] > h_rain_fall)
{
h_rain_fall = a_months[y];
position = y;
}
else if (a_months[y] > h_rain_fall)
{
printf("The system can't duplicated numbers\n");
}
else
{
//skip
}
}
switch (position) {
case 1:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", a, h_rain_fall );
break;
case 2:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", b, h_rain_fall );
break;
case 3:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", c, h_rain_fall );
break;
case 4:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", d, h_rain_fall );
break;
case 5:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", e, h_rain_fall );
break;
case 6:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", f, h_rain_fall );
break;
case 7:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", g, h_rain_fall );
break;
case 8:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", h, h_rain_fall );
break;
case 9:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", i, h_rain_fall );
break;
case 10:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", j, h_rain_fall );
break;
case 11:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", l, h_rain_fall );
break;
case 12:
printf("The month with the highest rain fall is %s with %d of rain
fall\n", m, h_rain_fall );
break;
default:
//skip
}
return(0);
}
"Profetas" <xu*****@yahoo.com> wrote: That is my solution
To me it is quite an ugly solution, with 12 different named variables
instead of an array, and that huge switch statement.
Here's my solution. It all works out very easily if you define a table using
an array of structs. Each line of the table stores a month's name and its
rainfall. I then use a pointer to keep track of the highest month found so
far.
#include <stdio.h>
/* the following line defines a handy macro to find out the *
* number of elements in an array. It takes the total size *
* of the array (in bytes) and divides that by the size of *
* a single element of the array. */
#define N(a) (sizeof (a) / sizeof *(a))
struct month /* this defines what each record contains */
{
const char *name; /* pointer to string literal for month name */
int rainfall; /* an int for the rainfall */
};
int main(void)
{
struct month array[] = /* now we define the table of months */
{ /* it is an array of 12 struct month */
{"jan", 23},
{"feb", 19},
{"mar", 38},
{"apr", 27},
{"may", 23},
{"jun", 24},
{"jul", 20},
{"aug", 18},
{"sep", 26},
{"oct", 30},
{"nov", 26},
{"dec", 22}
};
struct month *highest = &array[0]; /* point to first element */
size_t i;
for(i = 1; i < N(array); i++) /* start from the second */
{
if(array[i].rainfall > highest->rainfall)
{
highest = &array[i]; /* points to highest found so far */
}
}
printf("Highest rainfall in month %s with rainfall %d\n",
highest->name, highest->rainfall);
return 0;
}
--
Simon. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:
<?php
$simple = <<<END
<item>
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: shmartonak |
last post by:
For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?
If I'm using pointers to access array...
|
by: truckaxle |
last post by:
I am trying to pass a slice from a larger 2-dimensional array to a
function that will work on a smaller region of the array space. The
code below is a distillation of what I am trying to...
|
by: intrepid_dw |
last post by:
Hello, all.
I've created a C# dll that contains, among other things, two functions
dealing with byte arrays. The first is a function that returns a byte
array, and the other is intended to...
|
by: Stacey Levine |
last post by:
I have a webservice that I wanted to return an ArrayList..Well the service
compiles and runs when I have the output defined as ArrayList, but the WSDL
defines the output as an Object so I was...
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: anuptosh |
last post by:
Hi,
I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
|
by: vippstar |
last post by:
The subject might be misleading.
Regardless, is this code valid:
#include <stdio.h>
void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array = { {...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |