954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help figuring out the error in this code

hey, this is supposed to collect data into a 2 by 3 array. believe it or not, my teacher wrote this problem and he spent 20 minutes trying to figure out why it wasn't running properly. anyone care to help me figure out what was wrong with it??

#include <stdio.h>

int main () {
    int scores[2] [3];
    int row =2, col=3;
    int i, j;
for (i=0;i<=row;i++){
     for (j=0;j<=col-1;j++){
         printf ("enter number");
         scanf ("%d", scores[i] [j]);
     }
}
printf ("\n", scores [i] [i]);
return (0);
}
DarkPyros
Light Poster
26 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

lines 7 and 8 are incorect. Array elements are counted from 0 up, but not including the max index number. For example scores has valid index values of scores[0][] and scores[1][] The <= operator on line 7 will attempt to dereference scores[2][], which does not exist.

The correct code for line 7 would replace <= with just <.

line 10 is also in error. You have to pass a pointer to scores by adding & pointer operator. scanf("%d", &scores[i][j]);

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Even though AD explained the problem, I have to add to it:

hey, this is supposed to collect data into a 2 by 3 array. believe it or not, my teacher wrote this problem and he spent 20 minutes trying to figure out why it wasn't running properly. anyone care to help me figure out what was wrong with it??


Given that explanation, what are we supposed to be looking for? "...why it wasn't running properly" means what? What is unproper about it? What are we looking for?

"...what was wrong with it??" depend on what it's doing.

When you need help, please explain what we're looking for, why you claim it's not working. It really saves us from guessing.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

its collecting the data, but it is not putting it into the array, nor is it printing the array

DarkPyros
Light Poster
26 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Post current code.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

sorry about the late reply, my internet was down >.>

DarkPyros
Light Poster
26 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

sir found the error about a week ago..sad to say i lost the finished program

DarkPyros
Light Poster
26 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
sir found the error about a week ago..sad to say i lost the finished program


Man! Isn't that bad luck? Just when you found the error, you lost the program. Life ain't fair.
I'm glad you found the Internet, though! ;)

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: