Function doesn't do anything

I'm making a small program that makes a showable grid that can be modified with functions that create patterns on the grid. Right now I'm making a function fund that takes an object of class field; it's supposed to make a jagged pattern on the grid but right now I'm just trying to get it to make a straight line. And yet for some reason despite checking everything I know of that could cause a problem, the function never manages to modify the grid. Can you please take a look at my code and see what the heck is wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;

class field
   {
   public:
   int horz;
   int vert;
   int grid[1000][1000];
   field(int width,int height)
      {       
      horz=width;
      vert=height;    
      for (int i=0;i<vert;i++)
         {
         for(int j=0;j<horz;j++)
            {
            grid[j][i]=0;
            }
         }
      }
   field(int size)
      {
      horz=size; 
      vert=size;
      for (int i=0;i<vert;i++)
         {
         for(int j=0;j<horz;j++)
            {
            grid[j][i]=0;
            }
         }
      }
   void showgrid()
      {
      // Draws grid.
      cout<<endl;
      for (int u=0;u<vert;u++)
         {
         for (int p=0;p<horz;p++)
            {
            cout << grid[p][u];
            }
         cout<<endl;
         }
      cout<<endl;
      }
   };

void thund(field a)
   {
   int e=floor(a.vert/2);
   for (int v=0;v<a.horz;v++)
      {    
      a.grid[v][e]=1;
      }
   }
      
int main() 
   {
   srand(time(NULL));
   // Grid is created with imputted width and height then shown.
   int a,b;
   cout<<"Width:";
   cin>>a;
   cout<<"Height:";
   cin>>b;
   field Gamodified
   thund(Gary);
   Gary.showgrid();
   }
You are passing the parameter by value instead of by reference, so you modify the copy instead of the original.
Last edited on
Oh! I see what I did wrong. I now remember that to actually modify the argument itself, I have to put the & before the variable. Thank you for your help!
Topic archived. No new replies allowed.