Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Need some help with these programs for class!

 
Reply to this topicStart new topic

Need some help with these programs for class!

otiseoj
post 24 Aug, 2005 - 12:02 PM
Post #1


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

I am having trouble with some simple programs for class. Heres what he wants in the first program read om a list of 10 numbers. Then print the sum of the 10 numbers in the list. <Here's what I have"But its aint working">
CODE

#include<iostream.h>
      //opens the iostream functions which includes cin & cout
      main()
      //begins the main funtion
         {
           int i;int num[];int sum;
      //delcares variable i, num as an array of int's,and sum
               sum=0;
      //sets variable sum equal to 0
              for (i=0;i<=9)
      //starts for loop at 0 continues until it get to 9
                {
              cin>>num[i];
      //sends in the first data variable into num sub 0
              sum=sum+num[i];
      //calculates the total of numbers as there being sent through
              i++;
      //adds one to num[i] array to continue the loop
         }
              cout<<"The sum of the 10 number in this list is"<<sum;
      //prints out the sum of the 10 numbers in the array
                 }
The next program he wants us to read in the weight of 3 items. The program should print out the total weight for all 3 items. The program should read from a data file and work for any number of lines of data<"Once again I did it but it aint working heres what I have">


//CMPS201
//08-23-05

      #include<iostream.h>
      main()
      {
        int i;wt[];wt2[];wt3[];total[];

         while(i!=/n)
              {
                cin>>wt[i]>>wt2[i]>>wt3[i];
                total[i]=wt[i]+wt2[i]+wt3[i];
                 cout<<"The total weight for this group is "<<total[i];
                  i++;
              }
      }


The third and final program he wants us to write a program that will read in the number of quarters and nickels and print the total amount of money."For example 3 1 would be 80cents. Not worried about decimal pts.
Output should look like this 6 quarters and 2 nickels would be 160 cents.
sample data file
6 2
1 4
2 5
6 4
9 7
<"Again I know I didnt do this right but all Help is greatly appreciated">

<"heres what I have">
CODE

//CMPS201
//08-23-05

      #include<iostream.h>
      main()
      {
              int qur[i];nic[i];total[i];

      while (cin>>qur[i]>>nic[i])
      {
       totqur[i]=qur[i]*25;
       totnic[i]=nic[i]*5;
      cout<<qur[i]<<" "<<nic[i]<<" "<<"Would be
"<<totqur[i]+totnic[i]<<"cents."<<endl;
      i++;
      }
      }
User is offlineProfile CardPM

Go to the top of the page


Dark_Nexus
post 24 Aug, 2005 - 12:33 PM
Post #2


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


on your first program i am seeing this

CODE

int num[]


you have to declare an array with a constant, so int num[10] would probably suffice for this.

also, you're for loop should look something like this

CODE

for (i = 0;i < 9;i++)
{
    ///Stuff that is cool here
}


On the second program the same follows, all arrays must be declared with a constant

CODE

wt[];wt2[];wt3[];total[];


two things concerning the while loop for the second program

CODE

while (i != \n)


for comparisons to characters, you will need to take the literal of \n you can do this by placing it in single quotes

CODE

while (i != '\n')


but be aware that i is an integer, not a character, and the literal of \n will return some numbers you probably didn't intend to use.

as for the third program, the input doesn't look like the way you described it. the user will have to input like this

2
3 ^----Return

And as always arrays must be declared with constants tongue.gif
CODE

int qur[i];nic[i];total[i];


This post has been edited by Dark_Nexus: 24 Aug, 2005 - 12:34 PM
User is offlineProfile CardPM

Go to the top of the page

otiseoj
post 24 Aug, 2005 - 12:39 PM
Post #3


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

What if I dont know how many numbers are in the data file, say i wanna run it for infinite amount of numbers. and with that for statement in the second program I have the i++ at the end of the loop.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 24 Aug, 2005 - 12:44 PM
Post #4


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


i would just run through the data file one line at a time, so you don't have to allocate a ton of memory, after you have computed the amount of money, move to the next line, this make it so you only have to use 2 buffers.

after you output the total of each input line, you could go back through the output file and tally up the total of all lines.

another way to do this would be to, again, read in one line at a time, and keep a running total, as you move through each line, add the total of that input line to the total.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 24 Aug, 2005 - 12:45 PM
Post #5


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


that only applies to the third

your instructor may want you to use linkedlists or vectors, something along those lines. do either of those sound familar?
User is offlineProfile CardPM

Go to the top of the page

otiseoj
post 24 Aug, 2005 - 12:48 PM
Post #6


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

Can someone explain a little bit better, on each program. I just dont know why they wont work these are relatively easy programs I just hadnt coded in a couple months.
User is offlineProfile CardPM

Go to the top of the page

otiseoj
post 24 Aug, 2005 - 12:50 PM
Post #7


New D.I.C Head

*
Joined: 28 Feb, 2005
Posts: 18

hes using his own data file and we dont know how many lines of data will be in it but it will be in the form of 6 2
6 1
1 3
7 8
And also we havent started linked list or vectors. these seem like programs you wouldnt neccessarily have to use those for. They seem easy enought but I guess Im not looking at the big picture.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 24 Aug, 2005 - 01:10 PM
Post #8


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


the biggest thing i saw in each program was you were trying to declare arrays by using variables

CODE

int myarray[x];


this will not work, you have to use constants

CODE

int myarray[10];
;

and for reading in from files, you will need to use a different library,

#include <fstream>
User is offlineProfile CardPM

Go to the top of the page

zyruz
post 27 Aug, 2005 - 04:23 AM
Post #9


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


QUOTE(otiseoj @ Aug 24 2005, 01:50 PM)
hes using his own data file and we dont know how many lines of data will be in it but it will be in the form of 6 2
6 1
1 3
7 8
And also we havent started linked list or vectors. these seem like programs you wouldnt neccessarily have to use those for. They seem easy enought but I guess Im not looking at the big picture.

Does this mean that you need to read from a file?
if, so just do the calculation after a line, like:
CODE

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
   ifstream file;
   file.open("data.dat");
   int cent = 0, nicle = 0;
   while(file >> cent >> nicle)
   {
       cout << cent*25 + nicle*5 << '\n';
   }
}
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 13 Oct, 2006 - 03:53 PM
Post #10


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


otis,

Looking at your problem, the simplest way is zyrus way. But if you need to keep these values, you can either create a struct for a single linked list or use a vector that takes 2 ints as parameters, adding the lines to the vector as you go.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Oct, 2006 - 05:42 PM
Post #11


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


Excellent point...it may be a little late for otiseoj, but a good reccomendation
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 12:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month