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

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




Help comparing arrays

2 Pages V  1 2 >  
Reply to this topicStart new topic

Help comparing arrays

jona431
30 Nov, 2006 - 12:51 PM
Post #1

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions

i am new with arrays, and would appreciate any help with this. I have to compare the numbers in both arrays to see when the numbes in one array exceeds the numbers in the second array. I am first trying to get the array 1 to compare the numbers within itself and print out the highest number. here is my code and the errprs I am getting. thanks in advance for the help smile.gif

CODE
#include <stdio.h>
#include <conio.h>
#define STOCKS1 20
#define STOCKS2 20

int main()
{
    float exceededPrice=0;
    float stockPrice1=0;
    float rePort1();
    float stock1[]= {32.25, 40.50, 36.50, 40.00,
                    30.25, 30.25, 35.50, 36.00,
                    34.25, 37.00, 34.00, 35.00,
                    36.25, 34.25, 40.50, 41.50,
                    41.50, 40.00, 36.50, 50.34};

    float stock2[]= {40.25, 38.50, 34.50, 33.50,
                    30.50, 29.75, 37.50, 38.00,
                    34.75, 38.00, 34.25, 37.00,
                    34.25, 37.50, 34.50, 38.50,
                    37.50, 37.25, 38.25, 37.50};
    
    

    exceededPrice=stock1[0];
    for(stockPrice1 = 1; STOCKS1 < exceededPrice; stockPrice1++)
        if(stock1[1] > exceededPrice)
            exceededPrice = stock1[stockPrice1];
        printf("%.2f",exceededPrice);
    
    

    getch();
return 0;

}


error C2108: subscript is not of integral type
error C2440: '=' : cannot convert from 'float *' to 'float'
User is offlineProfile CardPM
+Quote Post

cipherence
RE: Help Comparing Arrays
30 Nov, 2006 - 12:57 PM
Post #2

D.I.C Regular
Group Icon

Joined: 1 Apr, 2006
Posts: 260



Thanked: 1 times
Dream Kudos: 650
My Contributions
maybe do a while loop, increasing the [] each time equally. as for the highest number within itself thing, i'm not sure.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help Comparing Arrays
30 Nov, 2006 - 12:57 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You must declare this variable as type integer to use in a FOR loop or as a subscript in an array.
CODE

int stockPrice1=0;


Also your FOR loop is incorrect, you need to use your counter as part of the condition in the loop.
CODE

for(stockPrice1 = 1; stockPrice1 < STOCKS1; stockPrice1++)

User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
30 Nov, 2006 - 01:26 PM
Post #4

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
got it...thanks! Here are the changes I have made so far...I'm posting as I go as I might need alittle morehelp later withthis project. I have to put this in a function...gonna see what I can 1st smile.gif

CODE
exceededPrice=stock1[0];
    for(stockPrice1 = 1; stockPrice1 < STOCKS1; stockPrice1++)
        if(stock1[stockPrice1] > exceededPrice)
            exceededPrice = stock1[stockPrice1];
        printf("%.2f",exceededPrice);

User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
30 Nov, 2006 - 06:55 PM
Post #5

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
ok now I have to do the average of the array. I have not done this before and cannot seem to find an example of it in my book or online. If someone could could give me a shove in the right direction I would appreciate it. Here is my code below.

CODE
#include <util.h>
#include <stdio.h>
#include <conio.h>
#define STOCK_SIZE 20


int main()
{
    int counter = 0;
    float Avg1;
    float Avg2;
    //float exceededPrice=0;
    //int stockPrice1=0;
    void rePort1(float [], float []);
    void rePort2(float [], float []);
    float stock1[STOCK_SIZE]= {32.25, 40.50, 36.50, 40.00,
                            30.25, 30.25, 35.50, 36.00,
                            34.25, 37.00, 34.00, 35.00,
                            36.25, 34.25, 40.50, 41.50,
                            41.50, 40.00, 36.50, 50.34};

    float stock2[STOCK_SIZE]= {40.25, 38.50, 34.50, 33.50,
                            30.50, 29.75, 37.50, 38.00,
                            34.75, 38.00, 34.25, 37.00,
                            34.25, 37.50, 34.50, 38.50,
                            37.50, 37.25, 38.25, 37.50};
    
    

    rePort1(stock1,stock2);
    rePort2(Avg1,Avg2);
    getch();
    return 0;

}

void rePort1(float Stock1[STOCK_SIZE], float Stock2[STOCK_SIZE])
{
            
    int counter=0; /* stock 1 larger than stock 2 */
    int i=0;

    int counter2=0; /* stock 2 larger than stock 1 */
    int j=0;

    int counter3=0; /* stocks == */
    int k=0;
    
    for(i = 0; i < STOCK_SIZE-1; i++)
        if(Stock1[i] > Stock2[i]) counter++;
        printf("number of days stock1 exceeded stock2 %d\n",counter);
    
    for(j = 0; j < STOCK_SIZE-1; j++)
        if(Stock1[j] < Stock2[j]) counter2++;
        printf("number of days stock2 exceeded stock1 %d\n",counter2);

    for(k = 0; k < STOCK_SIZE; k++)
        if(Stock1[k] == Stock2[k]) counter3++;
        printf("number of days stock1 equals stock2 %d\n",counter3);
    
}


void rePort2(float Avg1[STOCK_SIZE], float Avg2[STOCK_SIZE])
{
    /*int i;
    int stockAvg;

    printf("%.2f", Avg1[i]);
    */
    int i=0;
    int l=0;
    int counter=0;
    int average=0;

    for (i=0; l= STOCK_SIZE;);
        average = STOCK_SIZE/20;
    printf("Average of Stock 1 is %d", average);


}


This post has been edited by jona431: 30 Nov, 2006 - 06:56 PM
User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
30 Nov, 2006 - 08:02 PM
Post #6

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
I changed the code for the average, but I am still lost. Below is my code for the function. Thanks again

CODE
void rePort2(float CalcAvg1[STOCK_SIZE], float CalcAvg2[STOCK_SIZE])
{
    int i=0;
    float avg=0;
    float firstavg=0;

    for(i=0; i<STOCK_SIZE; i++)
     if (firstavg = avg + CalcAvg1[i]);
    firstavg = firstavg/CalcAvg1[i];
    printf("%.2f\n",avg);



}
blink.gif
User is offlineProfile CardPM
+Quote Post

wreckingcru
RE: Help Comparing Arrays
30 Nov, 2006 - 08:46 PM
Post #7

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 4


My Contributions
Your line "if (firstavg = avg + CalcAvg1[i]);" doesn't make any sense - why are you using an assignment statement inside an if statement? Did you mean
CODE
if (firstavg == avg + CalcAvg1[i])


This will run the next line
CODE
firstavg = firstavg/CalcAvg1[i];
if it is true.

To be frank, I'm not sure either way how this works...

Here is the way you calculate the average of an array - I would suggest you to paraphrase this example for your work

CODE

float avg = 0; //always make avg at least a float
int count = //size of array
int sum = 0;
//to calculate sum total of array elements; always initialize this to 0

for (int i=0; i<count; i++)
{
   sum = sum + array1[i];
//you could also say sum += array[i];
}  // these parentheses are optional since there is only one statement inside the loop, but that's for you to decide depending on your comfort level with the language

avg = sum/count;  //calculates the average

return avg;
  



Let me know if you need any further explanation. Cheers!
User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
1 Dec, 2006 - 04:29 PM
Post #8

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
ok, I changed my code up again...still not getting the output. I'm getting 0.00...when I manually calculate it I get 36.48. Can you see where I might be going wrong?.... again smile.gif

Thanks again in advance!!

CODE
void rePort2(float S_tocks1[STOCK_SIZE], float S_tocks2[STOCK_SIZE])
{
    
    int i, counter4;

    

    for (i=0; i<STOCK_SIZE; i++)
    int total1 = total1 + S_tocks1[i];

    int average1 = total1/STOCK_SIZE;

    for(i = 0; i<STOCK_SIZE; i++)
        if (S_tocks1[i]<average1)counter4++;
    printf("%.2f", counter4);


}    

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help Comparing Arrays
1 Dec, 2006 - 05:09 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
If you are going to use floats then in your calculations you also need to use floats. If you use integers to store the float values, you will receive a loss of precision. Basically you will lose anything after the decimal point.

Secondly if you are going to output the average, then output the average not the counter. wink2.gif Unless you have a specific reason for outputting the counter, in which case it should look like this printf("%d", counter4);, you need to use the correct format specifier in order to see the value.

Last but not least you must initialize your counter4 variable, otherwise you are incrementing whatever value was already stored in that memory location.
CODE

void rePort2(float S_tocks1[STOCK_SIZE], float S_tocks2[STOCK_SIZE])
{
    
    int i, counter4 = 0;

    

    for (i=0; i<STOCK_SIZE; i++)
    float total1 = total1 + S_tocks1[i];

    float average1 = total1/STOCK_SIZE;

    for(i = 0; i<STOCK_SIZE; i++)
        if (S_tocks1[i]<average1)counter4++;

    printf("%.2f", average1);


}

User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
1 Dec, 2006 - 07:02 PM
Post #10

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
...found the answer to this part smile.gif

This post has been edited by jona431: 1 Dec, 2006 - 08:52 PM
User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
2 Dec, 2006 - 10:45 AM
Post #11

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
I have a header file that was supplied to us. i saved it as a .h extension in the folder with the sourcefile. when I try to call it, it says that PrintTitle(); is an undeclaired variable. How do i use it, or did I saveit to the wrng spot. thanks smile.gif
User is offlineProfile CardPM
+Quote Post

jona431
RE: Help Comparing Arrays
2 Dec, 2006 - 11:58 AM
Post #12

D.I.C Head
**

Joined: 8 Apr, 2006
Posts: 55


My Contributions
here is the header file

CODE
/*  Source file:  util.h     */
/*  This file contains utility functions */

void PrintTitle ();
void PrintDate ();
void PauseScreen ();

void  PrintTitle ()    /*Function to display the company title*/
{
      printf("Bethazor's Stats Lair\n");
      printf("---------------------\n");
}

void PrintDate ()      /*Function to display current date */
{
    rintf("Date = %s", __DATE__); /*2 underscores precede and follow DATE */
}

void PauseScreen ()
{
    printf("Press any key to continue");
    getch();
}



and here i am attempting to use it. Please tell me what i am doing wrong. I haved it in the folder that has the source code, not the debug folder or the compiler folder. I also tried using it like this <util.h> and just util.h .


CODE
#include <stdio.h>
#include <conio.h>
#include "util.h"
#define STOCK_SIZE 20


int main()
{
    
    int counter = 0;
    //    float Avg1;
    //float Avg2;
    //float exceededPrice=0;
    //int stockPrice1=0;
    void rePort1(float [], float []);
    void rePort2(float [], float []);
    void rePort3(float [], float []);
    float stock1[STOCK_SIZE]= {34.25, 40.50, 36.50, 40.00,
                               30.25, 30.25, 35.50, 36.00,
                               34.25, 37.00, 34.00, 35.00,
                               36.25, 34.25, 40.50, 41.50,
                               41.50, 40.00, 36.50, 34.50};

    float stock2[STOCK_SIZE]= {40.25, 38.50, 34.50, 33.50,
                               30.50, 29.75, 37.50, 38.00,
                               34.75, 38.00, 34.25, 37.00,
                               34.25, 37.50, 34.50, 38.50,
                               37.50, 37.25, 38.25, 37.50};
    
    
    PrintTitle ();
    rePort1(stock1,stock2);
    rePort2(stock1,stock2);
    //getch();


This post has been edited by jona431: 2 Dec, 2006 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 06:53PM

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