Array of a type

P

Profetas

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);
if (a_months > h_rain_fall)
{
h_rain_fall = a_months;
}
else
{
//skip
}
}
printf("The highst rain fall is %d",h_rain_fall);
return(0);
}


Thanks Profetas
 
J

Joona I Palaste

Profetas said:
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);
if (a_months > h_rain_fall)
{
h_rain_fall = a_months;
}
else
{
//skip
}
}
printf("The highst rain fall is %d",h_rain_fall);
return(0);
}
 
A

Al Bowers

Profetas said:
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);
if (a_months > h_rain_fall)
{
h_rain_fall = a_months;
}
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);
if (a_months > h_rain_fall)
h_rain_fall = a_months;
}
printf("The highst rain fall is %d\n",h_rain_fall);
return 0;
}
 
R

Richard Bos

Joona I Palaste said:
Profetas said:
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};

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
 
P

Profetas

#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);
if (a_months > h_rain_fall)
{
h_rain_fall = a_months;
}
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
 
A

Al Bowers

Profetas said:
#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);
if (a_months > h_rain_fall)
{
h_rain_fall = a_months;
}
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
 
P

Profetas

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.
 
P

Profetas

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);
}
 
R

Ralmin

Profetas said:
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.rainfall > highest->rainfall)
{
highest = &array; /* points to highest found so far */
}
}
printf("Highest rainfall in month %s with rainfall %d\n",
highest->name, highest->rainfall);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top