• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 17:05
CEST 23:05
KST 06:05
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris34Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
BoxeR's Wings Episode 2 - Fan Translation Greatest Players of All Time: 2025 Update #1: Maru - Greatest Players of All Time A Eulogy for the Six Pool Geoff 'iNcontroL' Robinson has passed away
Tourneys
$5,000 WardiTV Summer Championship 2025 Maestros of The Game—$20k event w/ live finals in Paris $5,100+ SEL Season 2 Championship (SC: Evo) Esports World Cup 2025 Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
External Content
Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below
Brood War
General
Post ASL20 Ro24 discussion. No Rain in ASL20? BGH Auto Balance -> http://bghmmr.eu/ How do I speak directly to Coinbase?1-(888)-419-97 Recent recommended BW games
Tourneys
[ASL20] Ro24 Group E Small VOD Thread 2.0 [Megathread] Daily Proleagues [ASL20] Ro24 Group F
Strategy
Muta micro map competition Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s) Gtx660 graphics card replacement
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
How Culture and Conflict Imp…
TrAiDoS
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Customize Sidebar...

Website Feedback

Closed Threads



Active: 901 users

More help with C++

Blogs > KiLL_ORdeR
Post a Reply
Normal
KiLL_ORdeR
Profile Blog Joined June 2009
United States1518 Posts
November 18 2010 01:24 GMT
#1
Hey all,

You've been very, very helpful in the past, and I hope to draw on more of Team Liquid's vast experience with computer science and programming.

We just started doing arrays last week, and although they look complicated as hell at first, it's actually extremely straightforward, which is nice. I'm only having trouble with one program out of the four I need to do tonight, here it is:

Number Analysis Program

* Ask the user to enter a file name. Assume that the file contains a series of numbers, each written on a separate line. The program should read the contents of the file into an array and then display the following data:

- The lowest and highest numbers in the array
- The total numbers in the array
- The average of the sum of the numbers in the array

The Student CD contains a file named numbers.txt that you can use to test the program.

+ Show Spoiler [Code So Far] +
Don't have anything yet


First of all, I have absolutely no clue how to link a file to a program in this way. Maybe it's completely obvious and I'm overthinking this one, but that's the main part that's throwing me for a loop.

I'm confident that once I learn how to do this, I can write the code required to get the values asked for, but again, I don't know how to draw information from a file like that, so maybe there is something i'm missing.

Furthermore, since I bought the eBook version of the textbook, I didn't get the CD, so I don't have that test file. I can obviously just make text file in notepad to test, but i was like "WTF, pwned " when I saw that.

anyway, as always, any and all help is greatly appreciated and thanks in advanced for everyone who does help me out!

*
In order to move forward, we must rid ourselves of that which holds us back. Check out my stream and give me tips! twitch.tv/intotheskyy
KiLL_ORdeR
Profile Blog Joined June 2009
United States1518 Posts
Last Edited: 2010-11-18 01:28:24
November 18 2010 01:24 GMT
#2
Oh ya, and one more thing.

I use Wascana Eclipse as my editing tool, for those of you who haven't checked the previous blogs or forgot.

For some reason, a lot of the time when I make changes to a source code withing a project, I get this error that says "Cannot open output file NameOfFile.exe: Permission denied."

The problem is easily solved by just closing the project and reopening it, but that becomes a huge pain in the ass after the 2oth or so time. Also, it doesn't happen every time I make changes, only sometimes.

Do any Wascana users know how to make this stop? That would be doubly awesome as well.
In order to move forward, we must rid ourselves of that which holds us back. Check out my stream and give me tips! twitch.tv/intotheskyy
NevilleS
Profile Blog Joined July 2009
Canada266 Posts
November 18 2010 01:35 GMT
#3
Damn, introductory coding courses are so bad... Testing on file I/O instead of arrays, lol.

In c++, you should get used to using the standard library. The library code you are looking for, I believe, is ifstream (input file stream), which you can learn about by googling. It's a bit overwhelming if you haven't learned anything about objects, but look for a simple example of how to open a file and read ints out and you will be fine.

Afterwards it is loops and basic math, if you payed attention to how top access and lop through arrays that should be easy.
huameng
Profile Blog Joined April 2007
United States1133 Posts
November 18 2010 01:35 GMT
#4
http://www.cplusplus.com/doc/tutorial/files/

That seems like it'd teach you everything you need to know although I could be missing something considering I'm not much of an actual programmer. Just read in the filename as a string from standard in, then open up an input file stream with that string as the name and read in all the numbers with that stream's name instead of cin... algorithm after that should be the same.
skating
neSix
Profile Blog Joined November 2004
United States1772 Posts
November 18 2010 01:36 GMT
#5
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com
NoUShutUp
Profile Joined January 2008
United States172 Posts
November 18 2010 01:36 GMT
#6
Here's some help with File I/O for C++
http://www.cplusplus.com/doc/tutorial/files/
I think it will be a good place to start :D
Amnesia
Profile Blog Joined September 2009
United States3818 Posts
November 18 2010 01:39 GMT
#7
On November 18 2010 10:36 neSix wrote:
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com

My favorite site as well. It works wonders!
NoUShutUp
Profile Joined January 2008
United States172 Posts
November 18 2010 01:39 GMT
#8
On November 18 2010 10:36 neSix wrote:
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com


I understand what you are saying. I was a tutor for introductory programming classes in college and I've encountered many of these people. Just have to be patient and 'guide' them to do the work by themselves D:
Sexualinguistic
Profile Blog Joined September 2010
United States49 Posts
November 18 2010 01:42 GMT
#9
you can use a pair of nested loops to add the contents of each element to an accumulator.
ex.

const int NUM_ROWS = 3; //Number of Rows
const int NUM_COLS = 5; //Number of Columbs
int total = 0; //Accumulator
int numbers[NUM_ROWS][NUM_COLS] = {{numbers}};

//Sum the array elements.

for (int row = 0; row < NUM_ROWS; row++)
{ for (int col = 0; col < NUM_COLS; col++)
total += numbers[row][col];
}
//Display the sum.

cout<< "The total is " <<total <<endl;

I also need some help defining a 3D array(not to be a thread-derailer);

I need to display a 3D array in which has a bunch of numbers and has 3 subjects, 4 grades(9th-12th), and [Fail, C, B, A] in 3 boxes kinda like

Math
-----------------------
9th [Fail] [C] [B] [A]
10th [20] 29] [10] [9]
11th [31] [7] [15] [7]
12th [12] [13] [20] [14]

and I have to make 3 boxes like this, my code so far is:

//arrayz©.-·:._.·:·¯·:·_.~`z3r0~
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int Ar[3][4][4] = {20, 29, 10, 9, 31, 7, 15, 7, 12, 13, 20, 14, 7, 22, 11, 12, 22, 2, 33, 9, 19, 7, 7, 13, 5, 17, 12, 9, 2, 7, 21, 33, 33, 12, 7, 1, 10, 19, 3, 7, 14, 8, 9, 12, 9, 8, 20, 17};


return 0;
}
[Scorpion]
NevilleS
Profile Blog Joined July 2009
Canada266 Posts
November 18 2010 01:50 GMT
#10
On November 18 2010 10:39 NoUShutUp wrote:
Show nested quote +
On November 18 2010 10:36 neSix wrote:
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com


I understand what you are saying. I was a tutor for introductory programming classes in college and I've encountered many of these people. Just have to be patient and 'guide' them to do the work by themselves D:


Yeah, but honestly, it's not a big deal to help a new guy get used to googling what he needs to learn... Introductory programming courses never show their students the basic skills of how to articulate what it is they need, how to read articles...

But honestly, googling something like reading from a file will turn up a lot of results that require A LOT more knowledge about programming than this guy has, so you can't just tell him to f*$# off and teach himself everything about object oriented coding required to understand the documentation for a basic io library like iostream...
KiLL_ORdeR
Profile Blog Joined June 2009
United States1518 Posts
November 18 2010 01:51 GMT
#11
On November 18 2010 10:39 NoUShutUp wrote:
Show nested quote +
On November 18 2010 10:36 neSix wrote:
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com


I understand what you are saying. I was a tutor for introductory programming classes in college and I've encountered many of these people. Just have to be patient and 'guide' them to do the work by themselves D:


Ok I understand what you guys are saying. I'm not some dumbfuck college kid who pays nerds to do my work for me so I can go get hammered every night, and while the fact that you assumed this of me is offensive, it's also completely understandable since that seems to be the trend in college nowadays, and you obviously don't know me. I'm an International Business major as well as a Digital Arts minor, so this course is very relevant to me and I actually want to learn how to do this so that I have a good foundation moving forward.

First of all I want to state, I do not want you to write the code for me, and in fact that's the opposite of what I want you to do.

secondly I have 3 other programs for this specific assignment, two of which I've finished and the third i'm working on right now, I can post the code if that would make you feel better, but I felt it unnecessary since I've already finished/ am about to finish.

I said in the post that I had absolutely no idea how to do this, and that I couldn't find the information in my textbook. Links to places where I can get the information are very helpful, and I tried googling how to do it, but I wasn't 100% sure what words to write to get a proper google search.

as a side note, cplusplus.com is an awesome site, I found it tonight whe trying to look at how to do my last homework problem, so thanks to NoUShutUp for posting that.
In order to move forward, we must rid ourselves of that which holds us back. Check out my stream and give me tips! twitch.tv/intotheskyy
Cambium
Profile Blog Joined June 2004
United States16368 Posts
November 18 2010 01:53 GMT
#12
On November 18 2010 10:50 NevilleS wrote:
Show nested quote +
On November 18 2010 10:39 NoUShutUp wrote:
On November 18 2010 10:36 neSix wrote:
Asking for help on a homework assignment in which you've started absolutely no code is kind of like a big "hey fuck you write my program for me". What irks me even more about it is that you put your lack of code in a spoiler. Why in the world would you do that UNLESS you were trying to trick me into thinking you'd actually spent some time working on it yourself?

Having that said, break the problem up into pieces. Since you said you pretty much understand arrays, and you are having trouble just reading from the file, start by creating a simple array in memory with some hard-coded integers, and solve the problems you're asked to solve using that array.

Then once you have that working, all you need to do is figure out how to read from a file in C++. Here's a reference to a really big and often thorough textboox that I use to solve simple problems like "how do i read from a file?" in any language:

+ Show Spoiler [don't worry mine actually has co…] +

http://www.google.com


I understand what you are saying. I was a tutor for introductory programming classes in college and I've encountered many of these people. Just have to be patient and 'guide' them to do the work by themselves D:


Yeah, but honestly, it's not a big deal to help a new guy get used to googling what he needs to learn... Introductory programming courses never show their students the basic skills of how to articulate what it is they need, how to read articles...

But honestly, googling something like reading from a file will turn up a lot of results that require A LOT more knowledge about programming than this guy has, so you can't just tell him to f*$# off and teach himself everything about object oriented coding required to understand the documentation for a basic io library like iostream...


In this case, googling "Read File C++" gives you a very good website with crystal clear explanations.

http://www.google.com/search?q=read file c++&esrch=BetaShortcuts
When you want something, all the universe conspires in helping you to achieve it.
Sexualinguistic
Profile Blog Joined September 2010
United States49 Posts
November 18 2010 01:58 GMT
#13
On November 18 2010 10:42 Sexualinguistic wrote:
you can use a pair of nested loops to add the contents of each element to an accumulator.
ex.

const int NUM_ROWS = 3; //Number of Rows
const int NUM_COLS = 5; //Number of Columbs
int total = 0; //Accumulator
int numbers[NUM_ROWS][NUM_COLS] = {{numbers}};

//Sum the array elements.

for (int row = 0; row < NUM_ROWS; row++)
{ for (int col = 0; col < NUM_COLS; col++)
total += numbers[row][col];
}
//Display the sum.

cout<< "The total is " <<total <<endl;

I also need some help defining a 3D array(not to be a thread-derailer);

I need to display a 3D array in which has a bunch of numbers and has 3 subjects, 4 grades(9th-12th), and [Fail, C, B, A] in 3 boxes kinda like

Math
-----------------------
9th [Fail] [C] [B] [A]
10th [20] 29] [10] [9]
11th [31] [7] [15] [7]
12th [12] [13] [20] [14]

and I have to make 3 boxes like this, my code so far is:

//arrayz©.-·:._.·:·¯·:·_.~`z3r0~
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int Ar[3][4][4] = {20, 29, 10, 9, 31, 7, 15, 7, 12, 13, 20, 14, 7, 22, 11, 12, 22, 2, 33, 9, 19, 7, 7, 13, 5, 17, 12, 9, 2, 7, 21, 33, 33, 12, 7, 1, 10, 19, 3, 7, 14, 8, 9, 12, 9, 8, 20, 17};


return 0;
}


bump for interest
[Scorpion]
KiLL_ORdeR
Profile Blog Joined June 2009
United States1518 Posts
November 18 2010 02:02 GMT
#14
On November 18 2010 10:58 Sexualinguistic wrote:
Show nested quote +
On November 18 2010 10:42 Sexualinguistic wrote:
you can use a pair of nested loops to add the contents of each element to an accumulator.
ex.

const int NUM_ROWS = 3; //Number of Rows
const int NUM_COLS = 5; //Number of Columbs
int total = 0; //Accumulator
int numbers[NUM_ROWS][NUM_COLS] = {{numbers}};

//Sum the array elements.

for (int row = 0; row < NUM_ROWS; row++)
{ for (int col = 0; col < NUM_COLS; col++)
total += numbers[row][col];
}
//Display the sum.

cout<< "The total is " <<total <<endl;

I also need some help defining a 3D array(not to be a thread-derailer);

I need to display a 3D array in which has a bunch of numbers and has 3 subjects, 4 grades(9th-12th), and [Fail, C, B, A] in 3 boxes kinda like

Math
-----------------------
9th [Fail] [C] [B] [A]
10th [20] 29] [10] [9]
11th [31] [7] [15] [7]
12th [12] [13] [20] [14]

and I have to make 3 boxes like this, my code so far is:

//arrayz©.-·:._.·:·¯·:·_.~`z3r0~
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int Ar[3][4][4] = {20, 29, 10, 9, 31, 7, 15, 7, 12, 13, 20, 14, 7, 22, 11, 12, 22, 2, 33, 9, 19, 7, 7, 13, 5, 17, 12, 9, 2, 7, 21, 33, 33, 12, 7, 1, 10, 19, 3, 7, 14, 8, 9, 12, 9, 8, 20, 17};


return 0;
}


bump for interest


please make your own thread.
In order to move forward, we must rid ourselves of that which holds us back. Check out my stream and give me tips! twitch.tv/intotheskyy
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2010-11-18 02:16:05
November 18 2010 02:02 GMT
#15
On November 18 2010 10:58 Sexualinguistic wrote:
Show nested quote +
On November 18 2010 10:42 Sexualinguistic wrote:
you can use a pair of nested loops to add the contents of each element to an accumulator.
ex.

const int NUM_ROWS = 3; //Number of Rows
const int NUM_COLS = 5; //Number of Columbs
int total = 0; //Accumulator
int numbers[NUM_ROWS][NUM_COLS] = {{numbers}};

//Sum the array elements.

for (int row = 0; row < NUM_ROWS; row++)
{ for (int col = 0; col < NUM_COLS; col++)
total += numbers[row][col];
}
//Display the sum.

cout<< "The total is " <<total <<endl;

I also need some help defining a 3D array(not to be a thread-derailer);

I need to display a 3D array in which has a bunch of numbers and has 3 subjects, 4 grades(9th-12th), and [Fail, C, B, A] in 3 boxes kinda like

Math
-----------------------
9th [Fail] [C] [B] [A]
10th [20] 29] [10] [9]
11th [31] [7] [15] [7]
12th [12] [13] [20] [14]

and I have to make 3 boxes like this, my code so far is:

//arrayz©.-·:._.·:·¯·:·_.~`z3r0~
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int Ar[3][4][4] = {20, 29, 10, 9, 31, 7, 15, 7, 12, 13, 20, 14, 7, 22, 11, 12, 22, 2, 33, 9, 19, 7, 7, 13, 5, 17, 12, 9, 2, 7, 21, 33, 33, 12, 7, 1, 10, 19, 3, 7, 14, 8, 9, 12, 9, 8, 20, 17};


return 0;
}


bump for interest


It would be easier if you just define a struct/class to hold the three floats for subjects' grades, and make an array of that class.

Actually, I thought about it some more, that's not how you use a 3D array at all. A 3D array gives you n^3 space, where you only need 3n...
When you want something, all the universe conspires in helping you to achieve it.
EtherealDeath
Profile Blog Joined July 2007
United States8366 Posts
November 18 2010 02:26 GMT
#16
Well, it should be pretty obvious that the only "nontrivial" part of this assignment is the file input, as the actual computation is blazingly obvious. So, you just had to google C++ file I/O and use the basic stuff. I don't think you even need to use delimiters since the default delimiter is '\n' I believe... could be wrong, haven't used C++ in 5 years.

So, just nextline reads basically. You don't even need to store the entire read - if you think about it, as long as you have a separate one integer wide buffer for max, min, sum, count, then you can do the computations required. And besides, I/O time will dominate so you don't need a buffer any larger than that to prevent your CPU from being throttled by the I/O time, assuming you pipeline the I/O into the computation.
Kiante
Profile Blog Joined December 2009
Australia7069 Posts
November 18 2010 03:25 GMT
#17
The approach i would take is A) find some example code to open a file, and read line by line
b) modify to write numbers into an array (ie: write each line to an array element, initialise the array to the correct size etc)
c) write the simple algorithms they asked for
d) replace your hard coded file path with an element which is written by cin

voila you're done. if you need specific help with any of these steps, post the code you have so far and i'll be happy to help. not going to write code from scratch for you tho
Writer
rabidch
Profile Joined January 2010
United States20289 Posts
November 18 2010 04:14 GMT
#18
tips:

understand how input from the file is fed in
looks like a loop over arrays exercise. not too difficult (read up on loops and arrays duh) once you get the hang of them

also look up how to open files on those websites above. usually just googling the simple stuff helps
LiquidDota StaffOnly a true king can play the King.
Sexualinguistic
Profile Blog Joined September 2010
United States49 Posts
November 18 2010 17:15 GMT
#19
On November 18 2010 11:02 KiLL_ORdeR wrote:
Show nested quote +
On November 18 2010 10:58 Sexualinguistic wrote:
On November 18 2010 10:42 Sexualinguistic wrote:
you can use a pair of nested loops to add the contents of each element to an accumulator.
ex.

const int NUM_ROWS = 3; //Number of Rows
const int NUM_COLS = 5; //Number of Columbs
int total = 0; //Accumulator
int numbers[NUM_ROWS][NUM_COLS] = {{numbers}};

//Sum the array elements.

for (int row = 0; row < NUM_ROWS; row++)
{ for (int col = 0; col < NUM_COLS; col++)
total += numbers[row][col];
}
//Display the sum.

cout<< "The total is " <<total <<endl;

I also need some help defining a 3D array(not to be a thread-derailer);

I need to display a 3D array in which has a bunch of numbers and has 3 subjects, 4 grades(9th-12th), and [Fail, C, B, A] in 3 boxes kinda like

Math
-----------------------
9th [Fail] [C] [A]
10th [20] 29] [10] [9]
11th [31] [7] [15] [7]
12th [12] [13] [20] [14]

and I have to make 3 boxes like this, my code so far is:

//arrayz©.-·:._.·:·¯·:·_.~`z3r0~
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int Ar[3][4][4] = {20, 29, 10, 9, 31, 7, 15, 7, 12, 13, 20, 14, 7, 22, 11, 12, 22, 2, 33, 9, 19, 7, 7, 13, 5, 17, 12, 9, 2, 7, 21, 33, 33, 12, 7, 1, 10, 19, 3, 7, 14, 8, 9, 12, 9, 8, 20, 17};


return 0;
}


bump for interest


please make your own thread.


no, fuck you. There are enough "HELP WITH C++" threads, why do you think I said "please don't think of me as a thread derailer"

PLUS, I even got the code figured out (thanks to someone else in this thread), I needed to use a triple array, goes something like:

+ Show Spoiler +

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{
int arr[3][4][4] = {20,29,10,9,31,7,15,7,12,13,20,7,22,11,12,22,2,33,9,19,7,7,13,5,17,12,9,2,7,21,33,33,12,7,1,10,19,3,7,14,8,9,12,9,8,20,17};
string courses[3] = {"Math","English", "Science"};
string grade[4] = {" 9th Grade", "10th Grade", "11th Grade", "12th Grade"};
/*int arr[3][4][4];
int value;

for (int x = 0; x<3; x++)
{
for (int y = 0; y<4; y++)
{
for (int k = 0; k<4; k++)
{
cin>>value;
arr[x][y][k] = value;

}
}
}
cout<<endl;

*/
double pass=0.0;
double fail = 0.0;
double passrate = 0.0;
for (int x = 0; x<3; x++)
{

cout<<setw(7)<<courses[x]<<": F C B A"<<endl;
/*
if (x==0)
cout<<"Math: F C B A"<<endl;
else if (x==1)
cout<<"English: F C B A"<<endl;
else
cout<<"Science: F C B A"<<endl;
*/
cout<<"---------------------------------"<<endl;
for (int y = 0; y<4; y++)
{
cout<<setw(10)<<grade[y]<<" : |";
/*
if (y==0)
cout<<" 9th Grade : |";
else if (y==1)
cout<<"10th Grade : |";
else if (y==2)
cout<<"11th Grade : |";
else
cout<<"12th Grade : |";
*/

for (int k = 0; k<4; k++)
{
cout<<right<<setw(3)<<arr[x][y][k]<<"|";
if (k==0)
fail+=arr[x][y][k];
else
pass+= arr[x][y][k];

}
cout<<endl;

}
cout<<"---------------------------------"<<endl;
cout<<" Fail = "<<fail<<" Pass = "<<pass<< " Pass rate = ";
passrate = pass / (fail+pass) * 100;
cout<<passrate<<"%"<<endl<<endl;
pass = 0;
fail = 0;
}

return 0;
}



[b]User was temp banned for this post.
[Scorpion]
Marahumm
Profile Blog Joined February 2010
United States98 Posts
Last Edited: 2010-11-18 17:42:50
November 18 2010 17:39 GMT
#20
EDIT: Seems I don't look at forum names until AFTER I posted.

You didn't comment your code one bit! Commenting your code is essential when you are trying to figure out what exactly you want your code to do. It's especially necessary when you want others to take a look at it so they don't have to wrack their minds over what exactly you're doing.

To clean up your code you can definitely initialize more functions so that your main function isn't so complicated:

+ Show Spoiler +

void ReadFile(string sFile); // reads in a file and passes all integers to a vector
int LowestScore(); // loops through all scores and returns the lowest one
int HighestScore(); // loops through all scores and returns the highest one
int ScoreCount(); // returns the number of scores in the vector
float AverageScore(); // returns the average score from all elements in the vector


This way when you're debugging you won't have to scroll through ALL of your code to try and find out where your problem is. Each task has it's own function, that way if you run across a problem with, say, the average score, you can skip right to the AverageScore() function and figure out what's wrong.
Looks like you mashed some poor fellers dog, Sarge.
tofucake
Profile Blog Joined October 2009
Hyrule19082 Posts
Last Edited: 2010-11-18 18:16:09
November 18 2010 18:15 GMT
#21
On November 19 2010 02:39 Marahumm wrote:
EDIT: Seems I don't look at forum names until AFTER I posted.

You didn't comment your code one bit! Commenting your code is essential when you are trying to figure out what exactly you want your code to do. It's especially necessary when you want others to take a look at it so they don't have to wrack their minds over what exactly you're doing.

To clean up your code you can definitely initialize more functions so that your main function isn't so complicated:

+ Show Spoiler +

void ReadFile(string sFile); // reads in a file and passes all integers to a vector
int LowestScore(); // loops through all scores and returns the lowest one
int HighestScore(); // loops through all scores and returns the highest one
int ScoreCount(); // returns the number of scores in the vector
float AverageScore(); // returns the average score from all elements in the vector


This way when you're debugging you won't have to scroll through ALL of your code to try and find out where your problem is. Each task has it's own function, that way if you run across a problem with, say, the average score, you can skip right to the AverageScore() function and figure out what's wrong.

I'm going to stab you.



while(!file.EOF())
{
int a = line;
if(a > max) max = a;
if(a < min) min = a;
sum += a;
total++;
}


That's all you need for the loop (roughly). The file IO is more important to learn on your own. With (i)fstream you'll need to use atoi to convert string -> int. Good luck.
Liquipediaasante sana squash banana
Glacierz
Profile Blog Joined May 2010
United States1244 Posts
November 18 2010 21:13 GMT
#22
On November 19 2010 03:15 tofucake wrote:
Show nested quote +
On November 19 2010 02:39 Marahumm wrote:
EDIT: Seems I don't look at forum names until AFTER I posted.

You didn't comment your code one bit! Commenting your code is essential when you are trying to figure out what exactly you want your code to do. It's especially necessary when you want others to take a look at it so they don't have to wrack their minds over what exactly you're doing.

To clean up your code you can definitely initialize more functions so that your main function isn't so complicated:

+ Show Spoiler +

void ReadFile(string sFile); // reads in a file and passes all integers to a vector
int LowestScore(); // loops through all scores and returns the lowest one
int HighestScore(); // loops through all scores and returns the highest one
int ScoreCount(); // returns the number of scores in the vector
float AverageScore(); // returns the average score from all elements in the vector


This way when you're debugging you won't have to scroll through ALL of your code to try and find out where your problem is. Each task has it's own function, that way if you run across a problem with, say, the average score, you can skip right to the AverageScore() function and figure out what's wrong.

I'm going to stab you.



while(!file.EOF())
{
int a = line;
if(a > max) max = a;
if(a < min) min = a;
sum += a;
total++;
}


That's all you need for the loop (roughly). The file IO is more important to learn on your own. With (i)fstream you'll need to use atoi to convert string -> int. Good luck.


This. The guy before him cracks me up, why run the loop 4 times when u can solve everything in 1.
Sexualinguistic
Profile Blog Joined September 2010
United States49 Posts
November 30 2010 00:42 GMT
#23
My apologies...
[Scorpion]
Normal
Please log in or register to reply.
Live Events Refresh
Next event in 2h 55m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 500
IndyStarCraft 170
UpATreeSC 152
ProTech111
JuggernautJason95
CosmosSc2 1
StarCraft: Brood War
Calm 2892
ggaemo 240
BeSt 142
Dewaltoss 87
Aegong 32
scan(afreeca) 19
Dota 2
capcasts179
Counter-Strike
Stewie2K576
taco 469
Foxcn230
Heroes of the Storm
Liquid`Hasu539
Other Games
FrodaN4316
Grubby2246
summit1g1602
C9.Mang0107
ViBE104
Trikslyr76
Mew2King53
PPMD24
Organizations
StarCraft 2
angryscii 22
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 22 non-featured ]
StarCraft 2
• StrangeGG 27
• davetesta19
• LUISG 16
• musti20045 8
• Kozan
• sooper7s
• AfreecaTV YouTube
• intothetv
• Migwel
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• blackmanpl 51
• 80smullet 20
• iopq 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV397
League of Legends
• Doublelift2792
• TFBlade933
Counter-Strike
• imaqtpie1147
Other Games
• Shiphtur159
Upcoming Events
Replay Cast
2h 55m
LiuLi Cup
13h 55m
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
18h 55m
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
Big Brain Bouts
18h 55m
Iba vs GgMaChine
TriGGeR vs Bunny
Reynor vs Classic
Serral vs Clem
BSL Team Wars
21h 55m
Team Hawk vs Team Dewalt
BSL Team Wars
21h 55m
Team Hawk vs Team Bonyth
Code For Giants Cup
1d 1h
SC Evo League
1d 14h
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
Maestros of the Game
1d 18h
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
1d 20h
[ Show More ]
SC Evo League
2 days
Maestros of the Game
2 days
SHIN vs Creator
Astrea vs Lambo
Bunny vs SKillous
HeRoMaRinE vs TriGGeR
BSL Team Wars
2 days
Team Bonyth vs Team Sziky
BSL Team Wars
2 days
Team Dewalt vs Team Sziky
Monday Night Weeklies
3 days
Replay Cast
4 days
Sparkling Tuna Cup
4 days
LiuLi Cup
5 days
Replay Cast
6 days
The PondCast
6 days
RSL Revival
6 days
Maru vs SHIN
MaNa vs MaxPax
Liquipedia Results

Completed

CSL Season 18: Qualifier 1
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
Acropolis #4 - TS1
CSL Season 18: Qualifier 2
SEL Season 2 Championship
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
Skyesports Masters 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.