|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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! Code:
#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);
}
}
|
| Sponsored Links |
|
|
|
|||
|
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.
Code:
#include <stdio.h>
#include <stdlib.h> /* for system */
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 (Dollars, count);
DollarsToYen (Dollars, Yen, count);
displayData (Dollars, Yen, 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("%f", &Dollars[j]);
}
void DollarsToYen ( float Dollars[], float Yen[], int count )
{
int j;
for ( j = 0; j < count; j++ )
Yen[j]=(float)(Dollars[j]*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", Dollars[j], Yen[j]);
}
}
|
|
|||
|
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 |
|
|||||
|
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). |
| Sponsored Links |
|
|
|
|||
|
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 |
|
|||||
|
Quote:
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. |
|
|||
|
For something like this I'd really recommend using C++. Use C if you need fast or low-level code.
|
|
|||||
|
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2D Python Array - "List Indices Must Be Integers" (cannot access an array element) | Bertsche | Python | 3 | 07-07-2008 11:51 AM |
| filling an array with random #s | gaylo565 | C# Programming | 5 | 05-02-2008 12:21 PM |
| Array manipulation Question in C++ | JJJIrish05 | C and C++ | 1 | 03-08-2008 03:06 PM |
| Usage of array structures to increment letter instances of text | Yuriy M | C and C++ | 2 | 09-13-2007 11:49 AM |
| Python 2D array question | annannienann | Python | 3 | 04-23-2007 05:36 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |