0

With an input of an integer 4, for example, I'm trying to get the following output w/ the initializePoly function. (0, 0) (-1, 1) (-2, 4) (-3, 9)

Instead, I'm getting all 0s in the outputs' second value when I run the code. Any feedback much appreciated.

#include <stdio.h>
#include <stdlib.h>

struct point{
    int x;
    int y;
};

void printPoint(struct point);
void printPoly(struct point *, int);
void initializePoly(struct point *, int);

int main(void) {
    
    // Fill in your main function here
    struct point * polygon;
    int num;
    scanf("%d", &num);
    polygon = (struct point *) malloc(num * sizeof(struct point));
   
 initializePoly(polygon, num);
 printPoly(polygon, num);
 free(polygon);
        
}

void printPoint(struct point pt) {
    printf("(%d, %d)\n", pt.x, pt.y);
}

void printPoly(struct point *ptr, int N) {
    int i;
    for (i=0; i<N; i++) {
        printPoint(ptr[i]);
    }
}

// Write your initializePoly() function here
void initializePoly(struct point *pt, int num){

int i;
for(i=0;i<num;i++)
    pt[i].x = -i;
    pt[i].y = i*i;
}
  • 5
    Loops like you're missing brackets around the body of the `for` loop in `initializePoly`. The statement `pt[i].y = i*i;` is only executed once after the loop exits. – Brian61354270 Jul 31 '20 at 19:14
  • 1
    Note that if you declared `i` in the `for` statement itself (e.g. `for (int i=0;i – user3386109 Jul 31 '20 at 19:21
  • If you use an editor that automatically does indenting for you, then you might have noticed that the second line after the for statement was less indented than the first one. It always pays to have an editor that understands the language syntax. – bruceg Jul 31 '20 at 19:33
  • 1
    https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Jul 31 '20 at 19:37
  • 2
    Did you ever program in python? That is currently tripping you up. – Yunnosch Jul 31 '20 at 19:55
  • 1
    Does this answer your question? [Why is it considered a bad practice to omit curly braces?](https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces) – Yunnosch Jul 31 '20 at 19:58
  • @Yunnosch I visisted the old question you linked, and it frighteningly seems that the main consensus was against _unnecessary_ curly brackets. So it seems to suggest that **always** using them, the good habit that would have prevented the OP to fall into this pit, is wrong. Let's say this: using parenthesis only _when it is necessary_ is like _closing the door of your house only when you know that thieves are coming_. – Roberto Caboni Jul 31 '20 at 20:53
  • 1
    @RobertoCaboni I agree totally. The question I linked discusses the topic It explains what problems can occur with skipping the curlies. And that is the answer to this question. – Yunnosch Jul 31 '20 at 21:02

1 Answers1

0
void initializePoly(struct point *pt, int num){

int i;
for(i=0;i<num;i++)
    pt[i].x = -i;
    pt[i].y = i*i;
}

same as

void initializePoly(struct point *pt, int num){
  int i;
  for(i=0;i<num;i++) {
    pt[i].x = -i;
  }
  pt[i].y = i*i;
}

Use {} to iterate both assignments.

void initializePoly(struct point *pt, int num){
  int i;
  for(i=0;i<num;i++) {
    pt[i].x = -i;
    pt[i].y = i*i;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256