Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 576 577 [578] 579 580 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 902655 times)

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8655 on: December 12, 2015, 10:00:04 pm »

Code: [Select]
#include <iostream>
#include <string>

struct menuITEMS
{

   string menuProducts;
   double menuPrice;

};

using namespace std;

/*

FUNCTION PROTOTYPES HERE!!

double getbookPrices(double bookARRAY)

*/

double getbookPrices(double bookARRAY);

int main(){

   double bookARRAY[3];

   double Prices;

   Prices = getbookPrices(3);

   
   system("Pause");
   return 0;
}

double getbookPrices(double bookARRAY)
{
   double x = 0;

   for (x = 0; x < bookARRAY; x++)

   while (bookARRAY < 0)
   {
   
      cout << "No negative numbers allowed for a price." << endl;
      cin >> bookARRAY;
   }

   return bookARRAY;
}

The problem is 1)

Code: [Select]
cin>> bookARRAY
is trying to assign a double into the pointer to an array of doubles.

2) your function double getBookPrices(double bookARRAY) is trying to return a double and asks for a double.

Try moving the loop to the main function and do this

Code: [Select]
double getBookPrice()

//main
for (int i = 0; i < bookAARRAYlength /*whatever the array len is*/; ++i)
{
x = getBookPrice()

bookARRAY[i] = x;
}
//end main

double getBookPrice()
{
double x;
cout >> //stuff
cin << x;
return x;
}

and that should fix it.

Otherwise you have to pass the array by reference to the function and modify the array directly; you'd use a void function to do that, because you're not returning anything.

quick edit: Arrays are basically pointers in wrappers, and since bookARRAY is an array, it's a pointer. bookARRAY[0] points to the first element pointed to by bookARRAY; it's '
  • ' because C/C++ use offset notation, and the first element bookARRAY points to has offset zero. But that's not necessary to know.


SO SINCE bookARRAY IS AN ARRAY! It means it's not a double, and you can't pass it as a double. You have to treat it like a pointer. Which isn't the best idea for the code you're running, since you have to pass it by reference, which means that you'd have to change the function from returning double to a void function, which doesn't return a thing because you're modifying the values of bookARRAY directly.

Honestly, it'd be easiest to do everything in main unless you're required to run the extra function here.
« Last Edit: December 12, 2015, 10:06:13 pm by Gentlefish »
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8656 on: December 12, 2015, 10:07:47 pm »

1) What do you mean "pointer of the array".

2) I need main to send an array to the function. Said array is a double. The array I am sending is not filled and is only a certain length as a test. Once array is sent over I want to fill it IN the function.

I hope I'm explaining my intentions well enough in 2. I've tried making it a void and taking away return and it still dosen't work.

« Last Edit: December 12, 2015, 10:11:13 pm by 3man75 »
Logged

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8657 on: December 12, 2015, 10:23:00 pm »

I'm assuming at this point you know what a pointer is, and that's good.

If you know how to pass pointers to functions, that's what you need to do here.

Arrays are special pointers, basically. a 1-dimensional array is a single pointer that points to the first element in the array.
Code: [Select]
int arr[5];
declares a pointer to arr[0], which is an int. arr[1] is &arr[0] + 1, and so on. so arr is a pointer. And because you're passing the array as a pointer, any modification you make to the array in the function will modify the array. I think.

So your function declaration should be

Code: [Select]
void function(int arr[], int arrsize);
where arr[] is your bookARRAY, and arrsize is the array length.

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8658 on: December 12, 2015, 10:24:45 pm »

You misunderstand what an array is, 3man75. It's a sequence of things. An array of doubles has type "double[]", because it's not a double - it's a collection of them. C refers to them by pointers - that is, when you pass the array to a function, what you're doing is passing the memory address of the first thing in the array. C trusts you to know how long the array is, so all it needs to know is where it starts, although if you're being safe you'll want to do what Gentlefish recommends and pass the array length as well (so that your function can handle different-length arrays).

Each element of the array is a double. That is, each thing in it is. You select elements by their index - that is, by their order in the array. The first one has index 0, so if you want to do something with the first element, you would refer to bookARRAY[0]. To refer to the second, bookARRAY[1]. To add all the elements of an array, you would write a for loop that iterates through integers from 0 to the number of elements in the array, minus 1 (because it started at 0, not one), and uses that as the index.

Generally, that takes the form

Code: [Select]
int i;
for(i = 0; i < n; i++)
{
    some_function(array[i]);
}
where n is the length of your array
« Last Edit: December 12, 2015, 10:31:22 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8659 on: December 12, 2015, 10:28:19 pm »

I misunderstand, or he misunderstands? :P
Haha, I'm talking about arrays in C, which are handled like pointers because the name of the array refers to a pointer that points to the first item in the array, and the sequential memory spaces which contain the rest of the array, up to the length.

Funny things happen when you flow over the length of the array for that reason.

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8660 on: December 12, 2015, 10:30:56 pm »

He does, sorry. I forgot to edit that!

But yeah. Bounds checking is very important in general, and nobody likes buffer overflows (except the people who exploit them for nefarious gains and/or lulz).
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

jaked122

  • Bay Watcher
  • [PREFSTRING:Lurker tendancies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8661 on: December 13, 2015, 12:47:24 pm »

Or you could write a 2d array class and override the [] operator to take a tuple(or pair) of two integers as indices.


Code: [Select]
template<typename T> class array2d{
private:\\All the private things like the pointer to the array of type T(don't make it actually 2d, it's slower)
  int dim1,dim2;
public:
  T& operator[](const std::tuple<int,int> &index){
    //dereference the array
  }
  T operator[](const std::tuple<int,int> &index)const{
    //same thing, but more constant
  }
};


Optionally you could add another two integer parameters to the template so that you have a constant array size, which can be useful for checking out of bounds behaviors and the like(and passing the information along to functions that accept it at compile time).

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8662 on: December 13, 2015, 01:58:55 pm »

I hope when you say C you specifically mean C++.

Prof hasn't covered anything related to pointers just arrays and how to pass them.

So when passing an array I need to

1) Tell the compiler how long it is.

2) what to call it?

This next question is directed at Bauglir.



Generally, that takes the form

Code: [Select]
int i;
for(i = 0; i < n; i++)
{
    some_function(array[i]);
}
where n is the length of your array

Do I need this type of code in order to pass an entire array to a function? It feels more complex than what class showed even though the teacher seems to have not been on point with everything..there was a joke their. By the way if anyone has any tips on how to pass array with structs to functions then that'd be awesome.


Anyways I whitewashed my project and now I just want to send an array and then display shit. Sorry for the foul language I just really need a foot in the door.

Since I still don't know how to post code like you guys I'm putting it in spoilers again.
Spoiler (click to show/hide)

Logged

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8663 on: December 13, 2015, 03:49:04 pm »

Since I still don't know how to post code like you guys I'm putting it in spoilers again.

Type [code] at the beginning of your code. Then, at the end, type [/code] again. Or you can select your code in the text box, then click the button with a pound sign on it in the reply screen.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8664 on: December 13, 2015, 04:36:46 pm »

1. You can't intialize a struct with a double.

2. The argument type is still bookMaterial when it should be bookMaterial[]
You're not passing a struct, you're passing a list of structs.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8665 on: December 13, 2015, 04:47:41 pm »

1. You can't intialize a struct with a double.

2. The argument type is still bookMaterial when it should be bookMaterial[]
You're not passing a struct, you're passing a list of structs.

1. Really I can't use double? How about a float? This sorta kills the purpose of me making prices as a thing.

2.
Code: [Select]
int main(){


bookMaterial books[SIZE] = { 22, 22, 54 };

getbooks(bookMaterial[], 3);//books is their to pass a name while the number 3 is how big the array is.

system("Pause");
return 0;

[code/]

Like this?

3. [quote author=itisnotlogical link=topic=98412.msg6662568#msg6662568 date=1450039744]
[quote author=3man75 link=topic=98412.msg6662287#msg6662287 date=1450033135]
Since I still don't know how to post code like you guys I'm putting it in spoilers again.
[/quote]

Type [nobbc][code] at the beginning of your code. Then, at the end, type [/code] again.[/nobbc] Or you can select your code in the text box, then click the button with a pound sign on it in the reply screen.
[/quote]
Thank you  :)
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8666 on: December 13, 2015, 04:56:54 pm »

No, the TYPE of the argument in the declaration has to be bookMaterial[]. books is a variable of type bookMaterial[], not of type type bookMaterial.
If you were using vectors, the argument would be of type std::vector<bookMaterial>. For now you can just assume that's the same thing for arrays, just with different notation.
(This is why I like C#. It feels so much more intuitive there that I accidentally use its notation everywhere else)

Also, the compiler can't magically turn floats into arbitrary structs, what if your struct contained two floats? I don't even remember how you would make an array of some struct without using pointers tbh.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8667 on: December 13, 2015, 04:57:13 pm »

1. You can't intialize a struct with a double.

1. Really I can't use double? How about a float? This sorta kills the purpose of me making prices as a thing.

You can't initialize a struct as any number, class, other struct, string, pointer or anything else. Because they are not the struct. The compiler doesn't know that you want to assign the double to bookMaterial.pricing.

Imagine if someone asked you for a list of books to read, and you respond with "50$, 27$ and 29$". They would have absolutely no idea what you were trying to say.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Bauglir

  • Bay Watcher
  • Let us make Good
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8668 on: December 13, 2015, 06:17:22 pm »

This next question is directed at Bauglir.



Generally, that takes the form

Code: [Select]
int i;
for(i = 0; i < n; i++)
{
    some_function(array[i]);
}
where n is the length of your array

Do I need this type of code in order to pass an entire array to a function? It feels more complex than what class showed even though the teacher seems to have not been on point with everything..there was a joke their. By the way if anyone has any tips on how to pass array with structs to functions then that'd be awesome.
No, that code is only necessary to do something like add all the elements of the array. It has nothing to do with passing arrays. Sorry that was unclear. What it does is perform some_function() on each element of the array (in your case, each double in bookARRAY). To add them all, you'd want to do something like

Code: [Select]
double price = 0;
outside the loop, and then inside the loop replace some_function(array); with

Code: [Select]
price += bookARRAY[i];
Then what it means is that the loop will add each element of the array to price, which finds the sum of all elements in the array.

To pass an array, you simply need to declare the function as everybody's been telling you. Include the brackets. bookARRAY is of type double[], not double.

EDIT: To explain the loop a bit better. Think about how the program will execute when it reaches it. You have a counter, i (a common shorthand for "iterator" because this code appears a lot and programmers don't like the extra typing). The loop will end after i becomes equal to or greater than the length of the array. It starts at 0 and counts up by 1 at each iteration.

At each iteration, it will look at the ith element of the array bookARRAY and add it to price. An array's elements begin numbering at 0, so the first iteration with look at the 0th element of the array (that is, bookARRAY[0]). Now, from what I just said about how i changes over the course of the loop, you can see that it will select each element of the array exactly once.
« Last Edit: December 13, 2015, 06:25:02 pm by Bauglir »
Logged
In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.
“What are you doing?”, asked Minsky. “I am training a randomly wired neural net to play Tic-Tac-Toe” Sussman replied. “Why is the net wired randomly?”, asked Minsky. “I do not want it to have any preconceptions of how to play”, Sussman said.
Minsky then shut his eyes. “Why do you close your eyes?”, Sussman asked his teacher.
“So that the room will be empty.”
At that moment, Sussman was enlightened.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8669 on: December 13, 2015, 09:17:19 pm »

I don't think I understand. *sigh*

I've put the questions up on chegg also to help me understand how to pass an array to a function and someone was able to do it. I don't know why though but when I attempt to cycle through that code with a loop it just dosen't seem to work nor can I understand why they do what they do. Does anyone know any helpful videos or pages to read up on "How to pass arrays to functions"? I'm looking at Buckys currently but it's just not working for me.

By the way the code if your interested is as follows.

Code: [Select]
#include <iostream>
#include <string>

using namespace std;

const int SIZE = 3;

struct bookMaterial{

double pricing;

string nameOFbook;
};

void getbooks(bookMaterial books[], int SIZE);

//getbooks should be given an array but you gave just strucure. SIZE should be of int.
int main(){

//The way you initialized books was wrong. This is the correct way to do it.
bookMaterial books[3];

bookMaterial b;

b.pricing = 22.30;

books[0] = b;

b.pricing = 22.34;

books[1] = b;

b.pricing = 54.23;

books[2] = b;


getbooks(books, 3);//books is their to pass a name while the number 3 is how big the array is.

system("Pause");
return 0;
}

void getbooks(bookMaterial books[], int SIZE)
{ //In here I just display book names and the prices from above.
cout << "Stuff" << endl;
}
[code/]

EDIT: Okay for gods armoks sakes I"m doing exactly like in a video and apparently the parameter in my function named books in now undefined.


Code: [Select]
void getbooks(books[], int SIZE);



//getbooks should be given an array but you gave just strucure. SIZE should be of int.
int main(){

//The way you initialized books was wrong. This is the correct way to do it.
int books[3] = { 1, 2, 3 };



getbooks(books, 3);//books is their to pass a name while the number 3 is how big the array is.

system("Pause");
return 0;
}

void getbooks( books[], int SIZE)
{ //In here I just display book names and the prices from above.
cout << "Stuff" << endl;
[code/]

EDIT EDIT: Meltdown over. I was missing the data type on the parameter and the prototype.
« Last Edit: December 13, 2015, 09:32:16 pm by 3man75 »
Logged
Pages: 1 ... 576 577 [578] 579 580 ... 796