Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,933 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,862 people online right now. Registration is fast and FREE... Join Now!




finding the max and min values in an array

 
Reply to this topicStart new topic

finding the max and min values in an array

paintballfreak72
14 Mar, 2008 - 07:35 AM
Post #1

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

hi, i have been trying to figure out this problem for about 3 days now, with no luck.
the issue is that i need to find the max value and min value in an array called "rainfall" and display the values along side the month.

heres what i have so far.

CODE
#include <windows.h>
#include <iostream>
#include <process.h>
#include <iomanip>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr(int x, int y)
{
    COORD                       coordScreen = { x, y };
    DWORD                       cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO  csbi;
    DWORD                       dwConSize;
    HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '),
                               dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
                               dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
}

    void main(void)

{
    string month[12] = {"January", "Febuary", "March", "April",
        "May", "June", "July", "August", "September", "October",
        "November", "December"};
    float rainfall[12];
    float avg = 0.00;
    float sum = 0.00;
    int i;
    int y = 5;
    int x = i + 1;
    
    gotoxy(10,4);
    cout << "Month" << endl;

    gotoxy(20,4);
    cout << "Rainfall" << endl;

    for(i = 0; i < 12; i++)
    {
        y++;
        gotoxy(10,y);
        cout << month[i] << endl;

        gotoxy(20,y);
        cin >> rainfall[i];
    }

    for(i = 0; i < 12; i++)
    {
        sum = sum + rainfall[i];
    }

    avg = sum/12;

    /*  im not sure what code id need here to find the highest and lowest values

for(i = 0; i < 12; i++)
    {
        if(rainfall[i] > rainfall[x]);
    }

*/

    gotoxy(10,20);
    cout << "Average rainfall for 12 months is " << avg << "mm" << endl;
    gotoxy(10,21);
    
/*this is where i need to display the values and according months


cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
    gotoxy(10,22);
    cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;

*/

    _getch();
    }





thanks for any help/input here guys/girls.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Finding The Max And Min Values In An Array
14 Mar, 2008 - 07:42 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,228



Thanked: 40 times
Dream Kudos: 25
My Contributions
What's the problem? Are you getting a compiler error? unexpected output?
User is offlineProfile CardPM
+Quote Post

paintballfreak72
RE: Finding The Max And Min Values In An Array
14 Mar, 2008 - 07:53 AM
Post #3

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

QUOTE(Amadeus @ 14 Mar, 2008 - 08:42 AM) *

What's the problem? Are you getting a compiler error? unexpected output?



well at this point it builds and compiles fine, i need to figure out what the max and min values of "rainfall[]" and display it along with the month it corresponds to.
User is offlineProfile CardPM
+Quote Post

Stepler
RE: Finding The Max And Min Values In An Array
14 Mar, 2008 - 08:13 AM
Post #4

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 27



Thanked: 2 times
My Contributions
Function of search of the maximal number in a array:
CODE

float max_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m<*(a+i)) m=*(a+i);
return m;
}

Function of search of the minimal number in a array
CODE

float min_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m>*(a+i)) m=*(a+i);
return m;
}


Parameters of function are the pointer on a array and quantity of numbers in a array.
Function returns accordingly the maximal (minimal) value in array.
You only need to insert it into your code!

This post has been edited by Stepler: 14 Mar, 2008 - 08:14 AM
User is offlineProfile CardPM
+Quote Post

paintballfreak72
RE: Finding The Max And Min Values In An Array
14 Mar, 2008 - 08:34 AM
Post #5

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

QUOTE(Stepler @ 14 Mar, 2008 - 09:13 AM) *

Function of search of the maximal number in a array:
CODE

float max_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m<*(a+i)) m=*(a+i);
return m;
}

Function of search of the minimal number in a array
CODE

float min_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m>*(a+i)) m=*(a+i);
return m;
}


Parameters of function are the pointer on a array and quantity of numbers in a array.
Function returns accordingly the maximal (minimal) value in array.
You only need to insert it into your code!




thanks,

but its not working right, i get lots of errors

l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(73) : error C2601: 'max_arr' : local function definitions are illegal
l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(38): this line contains a '{' which has not yet been matched
l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(84) : error C2601: 'min_arr' : local function definitions are illegal
l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(38): this line contains a '{' which has not yet been matched
l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(98) : error C2065: 'm' : undeclared identifier

User is offlineProfile CardPM
+Quote Post

Stepler
RE: Finding The Max And Min Values In An Array
14 Mar, 2008 - 11:29 AM
Post #6

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 27



Thanked: 2 times
My Contributions
Errors should not be, functions work truly!
I have inserted these functions into your code, all works look!
CODE

#include <windows.h>
#include <iostream>
#include <process.h>
#include <iomanip>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;
void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr(int x, int y)
{
    COORD                       coordScreen = { x, y };
    DWORD                       cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO  csbi;
    DWORD                       dwConSize;
    HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '),
                               dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
                               dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
}
float min_arr(float*,int);
float max_arr(float*,int);

    void main(void)

{
    string month[12] = {"January", "Febuary", "March", "April",
        "May", "June", "July", "August", "September", "October",
        "November", "December"};
    float rainfall[12];
    float avg = 0.00;
    float sum = 0.00;
    int i;
    int y = 5;
    int x = i + 1;
    
    gotoxy(10,4);
    cout << "Month" << endl;

    gotoxy(20,4);
    cout << "Rainfall" << endl;

    for(i = 0; i < 12; i++)
    {
        y++;
        gotoxy(10,y);
        cout << month[i] << endl;

        gotoxy(20,y);
        cin >> rainfall[i];
    }

    for(i = 0; i < 12; i++)
    {
        sum = sum + rainfall[i];
    }

    avg = sum/12;

    /*  im not sure what code id need here to find the highest and lowest values

for(i = 0; i < 12; i++)
    {
        if(rainfall[i] > rainfall[x]);
    }

*/

    gotoxy(10,20);
    cout << "Average rainfall for 12 months is " << avg << "mm" << endl;
    gotoxy(10,21);
    cout<<"max="<<max_arr(rainfall,12)<<endl;
    gotoxy(10,24);
    cout<<"min="<<min_arr(rainfall,12)<<endl;
/*this is where i need to display the values and according months


cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
    gotoxy(10,22);
    cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;

*/

    _getch();
    }
float max_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m<*(a+i)) m=*(a+i);
return m;
}
float min_arr(float *a,int n)
{
float m;
m=*a;
for(short i=1;i<n;i+=1)
if(m>*(a+i)) m=*(a+i);
return m;
}

User is offlineProfile CardPM
+Quote Post

paintballfreak72
RE: Finding The Max And Min Values In An Array
15 Mar, 2008 - 08:45 AM
Post #7

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

what does this part do

cout<<"max="<<max_arr(rainfall,12)<<endl;
gotoxy(10,24);
cout<<"min="<<min_arr(rainfall,12)<<endl;

i mean the "min_arr(rainfall,12)"


thanks for the help biggrin.gif
User is offlineProfile CardPM
+Quote Post

paintballfreak72
RE: Finding The Max And Min Values In An Array
15 Mar, 2008 - 08:11 PM
Post #8

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 16

k so it works now, thanks stepler. but i still need to display the month for the highest and lowest months...im gunna work on it tonite and ill post back if i get anywhere
User is offlineProfile CardPM
+Quote Post

Stepler
RE: Finding The Max And Min Values In An Array
16 Mar, 2008 - 06:10 AM
Post #9

New D.I.C Head
*

Joined: 4 Mar, 2008
Posts: 27



Thanked: 2 times
My Contributions
I modified functions, now there is that it was needed!
CODE

#include <windows.h>
#include <iostream>
#include <process.h>
#include <iomanip>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;
void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr(int x, int y)
{
    COORD                       coordScreen = { x, y };
    DWORD                       cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO  csbi;
    DWORD                       dwConSize;
    HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '),
                               dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
                               dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
}
int min_arr(float*,int);
int max_arr(float*,int);

    void main(void)

{
    string month[12] = {"January", "Febuary", "March", "April",
        "May", "June", "July", "August", "September", "October",
        "November", "December"};
    float rainfall[12];
    float avg = 0.00;
    float sum = 0.00;
    int i;
    int y = 5;
    int x = i + 1;
    
    gotoxy(10,4);
    cout << "Month" << endl;

    gotoxy(20,4);
    cout << "Rainfall" << endl;

    for(i = 0; i < 12; i++)
    {
        y++;
        gotoxy(10,y);
        cout << month[i] << endl;

        gotoxy(20,y);
        cin >> rainfall[i];
    }

    for(i = 0; i < 12; i++)
    {
        sum = sum + rainfall[i];
    }

    avg = sum/12;

    /*  im not sure what code id need here to find the highest and lowest values

for(i = 0; i < 12; i++)
    {
        if(rainfall[i] > rainfall[x]);
    }

*/

    gotoxy(10,20);
    cout << "Average rainfall for 12 months is " << avg << "mm" << endl;
    gotoxy(10,21);
    //cout<<"max="<<max_arr(rainfall,12)<<endl;
    cout<<"Min rainfall in "<<month[min_arr(rainfall,12)]<<endl;
    gotoxy(10,24);
    cout<<"Max rainfall in "<<month[max_arr(rainfall,12)]<<endl;
    //cout<<"min="<<min_arr(rainfall,12)<<endl;
/*this is where i need to display the values and according months


cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
    gotoxy(10,22);
    cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;

*/

    _getch();
    }
int max_arr(float *a,int n)
{
float m;
int nn;
m=*a;
for(short i=1;i<n;i+=1)
if(m<*(a+i)) {m=*(a+i);nn=i;}
return nn;
}
int min_arr(float *a,int n)
{
float m;
int nn;
m=*a;
for(short i=1;i<n;i+=1)
if(m>*(a+i)) {m=*(a+i);nn=i;}
return nn;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month