Jump to content

Array troubles

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
51 replies to this topic

#1
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Hi,
I am having trouble with one of my assignments dealing with arrays. The program requires a conversion between dollars and yen for a max number of 100 deposits.

The program needs to place user input ($) into an array, then convert those $ into Yen, and then display the value of each deposit in both dollars and yen.

I have completed the program basics, but am running into difficulties on several spots.

1). I get an error "invalid operands to binary" in my conversion sub-routine
2). I cannot figure out how to display the data needed.
3). I am sure there a few other things that I am missing - any help would be great!

#include <stdio.h>

const int MAX_CURRENCYS = 100;

const float DOLLARS_TO_YEN = 102.2;


    int nums;

    float Dollars;

    float Yen;


void readDollars ( float Dollars[], int count );

void DollarsToYen ( float Dollars[], float Yen[], int count );

void displayData ( float Dollars[], float Yen[], int count );

int main()

{        

    float Dollars[MAX_CURRENCYS],

           Yen[MAX_CURRENCYS];      


    int count;

    printf("\n Enter the number of Deposits: ");

    	scanf("%d", &count);


    readDollars (float Dollars[], int count);

    

    DollarsToYen (float Dollars[], float Yen[], int count);

    

    displayData (float Dollars[], float Yen[], int count);

    

    system("pause");


}


void readDollars (float Dollars[], int count)

{

   int j;

   printf("Enter the  dollar amount for each deposit: ");

   for ( j = 0; j < count; j++ )

       scanf("%d", &Dollars);

}

void DollarsToYen ( float Dollars[], float Yen[], int count )

{

    int j;

    for ( j = 0; j < count; j++ )

    Yen=(float)(Dollars*DOLLARS_TO_YEN);


}

void displayData ( float Dollars[], float Yen[], int count )

{

    int j;

    printf("\nDollars\tYen\n");

    for (j=0; j<count; j++)


    {

        printf("    %7f     %12f \n", j + 1);

    }

}


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You're calling standard functions correctly, but for your own you're trying to be too tricky by half. You also need to realize what an array is: it is not a single item, but a collection of them; when looping, use the single items in the collection.
#include <stdio.h>

[COLOR="Blue"]#include <stdlib.h> /* for system */[/COLOR]


const int MAX_CURRENCYS = 100;

const float DOLLARS_TO_YEN = 102.2;


    int nums;

    float Dollars;

    float Yen;


void readDollars ( float Dollars[], int count );

void DollarsToYen ( float Dollars[], float Yen[], int count );

void displayData ( float Dollars[], float Yen[], int count );

int main()

{

    float Dollars[MAX_CURRENCYS],

           Yen[MAX_CURRENCYS];


    int count;

    printf("\n Enter the number of Deposits: ");

    	scanf("%d", &count);


    readDollars ([COLOR="Blue"]Dollars, count[/COLOR]);


    DollarsToYen ([COLOR="Blue"]Dollars, Yen, count[/COLOR]);


    displayData ([COLOR="Blue"]Dollars, Yen, count[/COLOR]);


    system("pause");


}


void readDollars (float Dollars[], int count)

{

   int j;

   printf("Enter the  dollar amount for each deposit: ");

   for ( j = 0; j < count; j++ )

       scanf("%f", [COLOR="Blue"]&Dollars[j][/COLOR]);

}

void DollarsToYen ( float Dollars[], float Yen[], int count )

{

    int j;

    for ( j = 0; j < count; j++ )

    Yen[COLOR="Blue"][j][/COLOR]=(float)(Dollars[COLOR="Blue"][j][/COLOR]*DOLLARS_TO_YEN);


}

void displayData ( float Dollars[], float Yen[], int count )

{

    int j;

    printf("\nDollars\tYen\n");

    for (j=0; j<count; j++)


    {

        printf("    %7f     %12f \n", [COLOR="Blue"]Dollars[j], Yen[j][/COLOR]);

    }

}



#3
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Woah. Interesting bit of code there... I recommend going over your C++ basics, because there are many silly mistakes in that code.

I think the easiest way to do this would be to change your DollarsToYen() function. The only parameter it should take is a pointer to your array (pass-by-reference to modify it) and iterate through each value, converting it to yen.

Also, when you call your function in main(), you shouldn't be initializing variables.

EDIT: Or just copy dcs' implementation, and hopefully learn something.

#4
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks so much for the help.

I guess I thought arrays in C were treated a lot like those in Matlab (where you can manipulate entire arrays)

The whole item by item though makes sense now, Thanks again

~AB

#5
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Don't try to learn to code off of Matlab, that particular piece of software is geared towards engineering and mathematics, rather than application development.

Hopefully everything is cleared up now. Arrays are generally the same in all languages (fixed length, use [x] to index them, etc...). Python is an exception, as it doesn't even implement arrays, it simply uses lists (which use arrays under the hood, but you probably won't ever see that lol).

#6
arctic_blizzard

arctic_blizzard

    Newbie

  • Members
  • Pip
  • 9 posts
lol - I was an engineering student for 2 years, so Matlab was the only coding I knew (other than a little html).

I have recently switched to an MIS program, and C was brand new to me

A quick question about Python: I have heard from several people that it is usually used as a direct replacement of C - should Python be on the top of my list for programming languages to learn?

I am currently learning C, VB, and will start Java next semester.

Thanks,

~AB

#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
**** VB, it is a waste of time.

#8
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts

arctic_blizzard said:

lol - I was an engineering student for 2 years, so Matlab was the only coding I knew (other than a little html).

I have recently switched to an MIS program, and C was brand new to me

A quick question about Python: I have heard from several people that it is usually used as a direct replacement of C - should Python be on the top of my list for programming languages to learn?

I am currently learning C, VB, and will start Java next semester.

Thanks,

~AB

LOL. American engineering program I suppose.

Python need not be on the top of your list, I'd put C++/C and Java before it. When Py3k is officially released, I'd look into it if you get time. VB is pretty much a waste of time.

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
For something like this I'd really recommend using C++. Use C if you need fast or low-level code.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
All of the above will depend on the type of engineering you are doing. Python is good for scripting within an OS, but not good for direct hardware interaction. C/C++/ASM are good for working directly with the hardware. VB is almost useless. Java has it's place, but probably not in your environment.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
But when learning to code, Java and Python are really good to start off with, because they are much higher level coding than c/c++, so even if they don't do anything useful for, it's good to start off learning them.

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
But when you're learning to code, a language that resembles English should be your first port of call. Unless you want to learn scary {//void();} syntaxes.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums