#2D Arrays and Functions
1564 messages ยท Page 2 of 2 (latest)
As in, smol cannot start with a value that is smaller than all the values it's going to be compared to
if you mean the value of x[0][0], yes
ye
i can try that
cause then
we have a number from the table
and in that row
its only gonna look for a number smaller
once i point the sign in the ohter direction of course
alright
double temp(int x[][7],int rows, int colms)
int max = 0;
int smol = x[0][0];
int rowAvg = 0;
for(int i = 0;i < rows;i++)
for(int j= 0;j < colms;j++)
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];```
so it should look at index 0
first number of the first row
and compare if the other numbers are smaller than it
and what numbers are smaller than it
should make smol that smol
other wrise if there are no other smol numnrs
it will be smol already
yeah that makes sense
that's it.
Wait
hm?
That actually won't do. lol
oh ๐
i think i also just realizded it might give me the sum of the whole table
if i do that
You need to define smol so that it's always the smallest of the week, not of the entire table
oh
So how do you that?
oh yeah thats also a problem i dind't think of lol
so it just has to go before collums
right
actually both of them do
wait no
r deals with all the rows
not again
i don't supose i can just write cout << endl
nah
figures
well from yesterday
of the other function
to make week print
like the way we wanted
it was before the last for loop
double temp(int x[][7],int rows, int colms)
int max = 0;
int smol = 0;
int rowAvg = 0;
for(int i = 0;i < rows;i++)
if(x[i][j] > max) max = x[i][j];
if(x[i][j] > smol) smol = x[i][j];
for(int j= 0;j < colms;j++)```
but uhm
this doesnt sound right
its also going through the rows
it isnt.
Anyways, I need to rest now, I'm going to travel in like, 5 hours, screw me 
Alright
Probably only going to get back here in Sunday, if you need help till there, I can
Alright Iโll try my best to figure it out
Have a safe flight then
thanks for the help so far
You're welcome, good night o/
night and ttyl
@merry owl Has your question been resolved? If so, type !solved :)
u still need help @merry owl
alright can you tell me where you're at
sure
what is the fourth function going to do
okay so
thats the question
this is my code so far
#include <iostream>
#include <cstdlib>
using namespace std;
void tempData(int x[][7],int rows,int colms){
for(int i = 0;i < rows;i++){
for(int j = 0;j < colms;j++){
x[i][j] = rand () % 90 + 10;
}
}
}
void tempDisplay(int x[][7], int rows,int colms){
for(int i = 0; i < rows;i++){
cout << "Week " << i + 1 << " : ";
for(int j = 0;j < colms;j++){
cout << x[i][j] << " ";
}
cout << endl;
}
}
string convertNum(int x){
string days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",};
if(x > 6 || x < 0) return "0";
return days[x];
}
double temp(int x[][7],int rows, int colms)
int max = 0;
int smol = x[0][0];
int rowAvg = 0;
for(int i = 0;i < rows;i++)
for(int j= 0;j < colms;j++)
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
double avgTempWeek (){
}
int main()
{
int arr [4][7];
tempData(arr,4, 7);
tempDisplay(arr,4,7);
return 0;
}```
bruh that posted before the question ๐
i'll just screenshot what im focusing at
uhh which function is it?
double tmep
oops
double temp(int x[][7],int rows, int colms)
int max = 0;
int smol = x[0][0];
int rowAvg = 0;
for(int i = 0;i < rows;i++)
for(int j= 0;j < colms;j++)
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];```
this one
also for a quick suggestion, in the convertNum function, I would personally return something like "Error: incorrect input"
hmm okay i see that the smol variable isn't correct
yeah
i guess the max one is then ๐ค
hm i thought they might have the same porblme
cause both are going through the entire array
and i need tome both to go through only one row
you're setting smol to the first element of the first array in in x[][7]
yup
well you want to check the entire array
so the ifs are correct
ye
can you send a screenshot of all the instructions
okay so how could you set smol to the first element of each array in x?
btw, you're going to want to use curly braces for the for loops and if
of each row u mean?
yepp
yeah
my smol vairble holds x[0][0]
first row and first number in the row
but i guess for this to work
like u said
i need all the rows
lets change that to
x row and first number in row
so x[i][0]?
try
double temp(int x[][7],int rows, int colms){
int max = 0;
int smol = x[i][0];
int rowAvg = 0;
for(int i = 0;i < rows;i++){
for(int j= 0;j < colms;j++){
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
}
oh right curly brakcets
not gonna compile btw
and for the function as well
so how come
i isnt defined up there
right so i hav to declare it
its like reading a book left to right, top to bottom
you want the variable smol to equal the first element in each row
ye
do you think loops will be needed or no
no
but i is only declared in those loops
unless i leave it blank but i doubt that would work
since you want the first element of each row, you need a loop.
no
what would happen
but then wdym by this
#include <iostream>
int main() {
int x[3][3] = { {0, 4, 1}, {3, 1, 1}, {7, 9, 10} };
int max = 0;
int min = x[0][0];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (x[i][j] > max) {
max = x[i][j];
}
if (x[i][j] < min) {
min = x[i][j];
}
std::cout << "min: " << min << " " << "max: " << max << " ";
}
std::cout << "\n";
}
}
well i have it the same as yours
right now
it's incorrect
do you see the output of min
;compile
min: 0 max: 0 min: 0 max: 4 min: 0 max: 4
min: 0 max: 4 min: 0 max: 4 min: 0 max: 4
min: 0 max: 7 min: 0 max: 9 min: 0 max: 10
so why's it like that
u wrote it in the mian fuction too
but probably a small diference
i mean after the inner for loop
that doesnt really matter in this instance
is it supposed to count how the columns?
u need both so that smol can go through the collums
not count what i mean is
go throuh the collums
each collums in one row
goes throuhg it
but it can't do that if its outside the inner loop
but the for loop goes throughthe enturre array
ye
this is the 2d array for example
int x[3][3] = { {0, 4, 1}, {3, 1, 1}, {7, 9, 10} };
right
what are the smallest numbers in each row
ye so if we set it to x[0][0] at the beginning and check thru the whole array it will be checking against 0
so we gotta find a way to set it to the first number of each row
oh yeah never though about if the min was zero
so that it sets it to 0 for the first row, then 3 the second, then 7 for the third
hmm
if (x[i][j] < min) {
min = x[i][j];
}```
this part
the porblem rn
is what min is holding yes?
in your code its the first temperature of the entire array
i mean it shouldn't matter
ah okay
lets see in my code
double temp(int x[][7],int rows, int colms){
int max = 0;
int smol = x[i][0];
int rowAvg = 0;
for(int i = 0;i < rows;i++){
for(int j= 0;j < colms;j++){
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
}```
incorrect part is the smol initialization
okk
cool
for week 2, we need to set it to the first element; 32
and week 3; 61
etc
right but to unlokc every row, i know i must need a vairble
is there a pattern to this
this is true
to be able unclock each row..
i thought that would be the same thing as unlokcing every row
what variable should you use
what does unlocking mean
in this case
the array i mean
ye
x[][]
rn i have x[i][0]
and that i thought would give me the first number of each row
good, thats correct. you need to put it where i is in scope
ok
or maybe u mean after the first loop
where i is instiizled thats where it should go
but then what about the collums
what about it
wait actaully
the 0
already tells me
what place int he collums
so maybe its possibke
so rn now this is what i think your telling me to do
double temp(int x[][7],int rows, int colms){
int max = 0;
int rowAvg = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
}```
why did u move the if
but tis wrong right
it was fine down there
yes i misinterpretted then
^
oh wait the vairble
not the if
i just put the wrong one there
like that?
i edited it
uhh i think that might work
ye
double temp(int x[][7],int rows, int colms){
int max = 0;
int smol;
int rowAvg = 0;
for(int i = 0;i < rows;i++){
smol = x[i][0];
for(int j= 0;j < colms;j++){
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
}
i would do this personally in case i wanna use smol later
nah it shouldnt matter to muc tho
alright
but ye that means now both the max and smol funtion should work
oh wait i do need to print it out like this when i call this function
so i guess i have to put some cout here
or i could do it in the main function
wait no its repaeating
so might have to afterall
wait i should porbably deal with the avergaeg first lol
before i worry about pritning
ok so
double temp(int x[][7],int rows, int colms){
int max = 0;
double rowAvg = 0.0;
int sum = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
double rowAvg = sum/colms;
}```
dis the code so far
now i need to get the avg
of each row
so that should be adding the sum of each row
and diviing by the amount of collums
i need a sum vairble thouhg
okay not to figure out how to only add each row
and not the whole thing
ok i think that has to be after both after all
cause it needs to account for collums
so a simple sum =+ x[i][j] hopefully
and then average
so i think that needs to be outside
the for loop
alright thats what i have
somethign seems off though
if the sum varible is inside the for loop
is it gonna hold even after?
well it should
its a varible
and its in the function
but everything closes after a for loop
what do u think
whats the question
what are ur thoughts on the sum and avg part
double temp(int x[][7],int rows, int colms){
int max = 0;
double rowAvg = 0.0;
int sum = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
double rowAvg = sum/colms;
}```
thought i only want the sum of each row
not gonna compile
try
i didn't write anything in main fucntion
i can just write this right
cout << temp(arr,4,7);```
than again
the purpose of this function is to get the high and low and avg
those numbers should porbably be a specific row
double
try it
ye
whatever the heck that is
u know maybe i did something wrong in my main fucntion
can you guess why it may do that
what are you trying to cout
ye
you cannot
okay so
double temp(int x[][7],int rows, int colms){
int max = 0;
double rowAvg = 0.0;
int sum = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
rowAvg = sum/colms;
}```
how would u
oh wait it only return one thing
with those if statments
but then how would i get three things out of this funciton
cout?
perhaps
after the second for loop
i could go cout
and then max, min and rowavg
but that reminds me
its not like its a void function
so it does have to return something
whatever that may be but ad for the other things, would just cout them work?
Heyo lol hope ur not too sleep deprived
ye since it was thankgiving week, i got some relaxing going on
but now i'm back to work lol
Ah working on 2D arrays, that took me longer than it shouldโve to understand lol
lol ye
to catch u up
thats the quesiton
if it would show up fastee
and thats my code so far
#include <iostream>
#include <cstdlib>
using namespace std;
void tempData(int x[][7],int rows,int colms){
for(int i = 0;i < rows;i++){
for(int j = 0;j < colms;j++){
x[i][j] = rand () % 90 + 10;
}
}
}
void tempDisplay(int x[][7], int rows,int colms){
for(int i = 0; i < rows;i++){
cout << "Week " << i + 1 << " : ";
for(int j = 0;j < colms;j++){
cout << x[i][j] << " ";
}
cout << endl;
}
}
string convertNum(int x){
string days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",};
if(x > 6 || x < 0) return "0";
return days[x];
}
double temp(int x[][7],int rows, int colms){
int max = 0;
double rowAvg = 0.0;
int sum = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
}
}
rowAvg = sum/colms;
}
double avgTempWeek (){
}
int main()
{
int arr [4][7];
tempData(arr,4, 7);
tempDisplay(arr,4,7);
cout << temp(arr,4,7);
return 0;
}```
rn i'm wokring on the fourth function
double temp
double temp(int x[][7],int rows, int colms){
int max = 0;
double rowAvg = 0.0;
int sum = 0;
for(int i = 0;i < rows;i++){
int smol = x[i][0];
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > max) max = x[i][j];
if(x[i][j] < smol) smol = x[i][j];
cout << "Highest Temperature:" << max << endl;
cout << "Lowest Temperature: " << smol << endl;
}
}
rowAvg = sum/colms;
cout << "Average Temperature: " << rowAvg << endl;
}```
and thats what i have
Ah okay I get the instructions
Codeโs gonna take me a bit to read BC Iโm slow at reading code
ye lol take ur time
right so @oak shell would it work if i just cout the max , min and avg?
though i kinda need that happening mutiple time so it would have to be in the loop
4*7 prob
oh
so i can't use those number in the mian
seven porbably has to stay
since its the collum box
oh wait i put 1
and got these
those amount looks more resoable now
well more or less
only one avg
ok maybe the collums do need to chnage
no that wouldnt make sense
why
well i mean i want it to go throuhg 4 rows and 7 collums
its giving me so many numbers when its supose to gime me 3 numbers from one row
still no clue why this shows up either
what other numbers would you want to use
but i wanna know why
from what i can tell
this fourht function is supose to go throuhg given rows and collums
and grab 3 numbers from there
does the rows and collmus varible in the parameters determine how much of the orw and collum it can go to?
well i used 4 for cluums and shorter numbers appeared
also i dont thhink its doing th ecalculaiton right either
whys that, what numbers are expected?
idont know what other high and low numbers they are getting
but its scarmbled
first row
high number is right
though the second line for low temp is 83 which isnt true
but at the same time i dont think that line is that row
it seems scattered
but the 4th line
26
thats the smallest value in row 0
wait its also
printing the same numbers ๐
i just noticied
well more or less
actaully lemme try put 1 and 7
putting 1 and 7
it should only touch the first row
and give three numbers
but it gives 15 things
I guess that means i messed up somewhere with theorder of the cout
for sure
I'm gonna try to figure this out all tomrorow
I think i'm just gonan go to sleep for now
my brain is too fried rn lol
goodnight everyone
thank u all for helping
ttyl
Idk but the last output looks like a trash value, meaning ur accessing past an array or didnโt write to the last element in an array
Okay same goodnight
ye probably I'll hopefully figure it out tomrorow
ye night
@merry owl Has your question been resolved? If so, type !solved :)
ok i chnaged some things but i got it to work
void temp(int x[][7],int rows, int colms){
double rowAvg = 0.0;
for(int i = 0;i < rows;i++){
int smol = 0;
int max = 0;
int sum = 0;
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > x[i][max]) max = j;
if(x[i][j] < x[i][smol]) smol = j;
}
cout <<" -- " << "Week " << i + 1 << " -- " << endl;
cout << "Highest Temperature: " << x[i][max] << "(" << convertNum(max) << ")" << endl;
cout << "Lowest Temperature: " << x[i][smol] << "(" << convertNum(smol) << ")" << endl;
rowAvg = sum/7.0;
cout << "Average Temperature: " << rowAvg << endl;
}
}```
W
ye
i did the last one too
void avgTempWeek (int x[][7],int rows,int colms){
double colmAvg = 0.0;
for(int j = 0;j < colms;j++){
int sum = 0;
for(int i= 0;i < rows;i++){
sum += x[i][j];
}
colmAvg = sum/4.0;
cout << "--" << "Average by Weekday " << "--" << endl;
cout << convertNum(j - 1 + 1) << ":" << colmAvg << endl;
}
}```
it seems to work
but i'm wondering if i'm missing anything cause i can be blind
but it prints out this
so yeah it looks like there isn't any thing that shouldn;t be there
oh wait that first line isn't supose to print mutiple times
it's printing avg by weekday multiple times
okay lets see
void avgTempWeek (int x[][7],int rows,int colms){
double colmAvg = 0.0;
for(int j = 0;j < colms;j++){
int sum = 0;
cout << "--" << "Average by Weekday " << "--" << endl;
for(int i= 0;i < rows;i++){
sum += x[i][j];
}
colmAvg = sum/4.0;
cout << convertNum(j - 1 + 1) << ":" << colmAvg << endl;
}
}```
so maybe it goes inside the inner for loop
no lol it prints it four times ๐
so i guess after the first for loop maybe
got it
so above both for loops
which makes sense
dont want it to repeat
whats the point in subtracting by one then adding by one
well my intention at first was to keep make it go through the collums
with the plus 1
but then the index started with 1
with the j
and i need 0
so thats why lol
here is the whole code btw
#include <iostream>
#include <cstdlib>
using namespace std;
void tempData(int x[][7],int rows,int colms){
for(int i = 0;i < rows;i++){
for(int j = 0;j < colms;j++){
x[i][j] = rand () % 90 + 10;
}
}
}
void tempDisplay(int x[][7], int rows,int colms){
for(int i = 0; i < rows;i++){
cout << "Week " << i + 1 << " : ";
for(int j = 0;j < colms;j++){
cout << x[i][j] << " ";
}
cout << endl;
}
}
string convertNum(int x){
string days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",};
if(x > 6 || x < 0) return "0";
return days[x];
}
void temp(int x[][7],int rows, int colms){
double rowAvg = 0.0;
for(int i = 0;i < rows;i++){
int smol = 0;
int max = 0;
int sum = 0;
for(int j= 0;j < colms;j++){
sum += x[i][j];
if(x[i][j] > x[i][max]) max = j;
if(x[i][j] < x[i][smol]) smol = j;
}
cout <<" -- " << "Week " << i + 1 << " -- " << endl;
cout << "Highest Temperature: " << x[i][max] << "(" << convertNum(max) << ")" << endl;
cout << "Lowest Temperature: " << x[i][smol] << "(" << convertNum(smol) << ")" << endl;
rowAvg = sum/7.0;
cout << "Average Temperature: " << rowAvg << endl;
}
}
void avgTempWeek (int x[][7],int rows,int colms){
double colmAvg = 0.0;
cout << "--" << "Average by Weekday " << "--" << endl;
for(int j = 0;j < colms;j++){
int sum = 0;
for(int i= 0;i < rows;i++){
sum += x[i][j];
}
colmAvg = sum/4.0;
cout << convertNum(j - 1 + 1) << ":" << colmAvg << endl;
}
}
int main()
{
int arr [4][7];
tempData(arr,4, 7);
tempDisplay(arr,4,7);
temp(arr,4,7);
avgTempWeek(arr,4,7);
return 0;
}```
what's the difference between convertNum(j - 1 + 1) and convertNum(j)?
yes lol
cause everything feels unpredictable
sometimes
okay i guess i can just leave the j there lol
ah ok
but ye i think i've covered anything
i don't i'm missing anythign else
lemme me know if u notice anything though lol cause i can be blind
but ye i guess this means i'm done with it
show the whole output
ye i'll do it by like one spot or something
i just thats just cout uh space
in the main function
okay seems like it matches now
Missing the first line
ih yeah
oh yeah i have that nowhere written thats weird
but i guess thats the second function
okay there
oh alright
Also here
well its randomized
space on the left probably
its missing a space
between the num and the open parenthesis
also it should be 3 dashes, like so: --- you have 2
oh okay that one
also no space on the left
the other ones stay two
all 3
i can't really see the picture but you have it
nice! i think everythings sorted
great
I'm finnaly done
aftet two days
oh wait no 2 days and a half
but yee i'm doneee
oh ye also thank you for ur help
could not have gotten this far without you all
I'll close the post then
Have a nice day yee
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity