Partial array output includes garbageThe Definitive C Book Guide and ListWhat is the difference between #include <filename> and #include “filename”?How do I determine the size of my array in C?How to initialize all members of an array to the same value?With arrays, why is it the case that a[5] == 5[a]?Detecting superfluous #includes in C/C++?C pointer to array/array of pointers disambiguationWhat is array decaying?C/C++ include header file orderFastest sort of fixed length 6 int arrayin a struct, scanf returns var address not value
Sort with assumptions
Output visual diagram of picture
What is this high flying aircraft over Pennsylvania?
Is there a distance limit for minecart tracks?
C++ lambda syntax
Reasons for having MCU pin-states default to pull-up/down out of reset
Is divisi notation needed for brass or woodwind in an orchestra?
What's the meaning of "what it means for something to be something"?
Connection Between Knot Theory and Number Theory
Why would five hundred and five same as one?
Do people actually use the word "kaputt" in conversation?
What is the meaning of "You've never met a graph you didn't like?"
Why can't I get pgrep output right to variable on bash script?
Toggle window scroll bar
Why doesn't Gödel's incompleteness theorem apply to false statements?
A seasonal riddle
Trouble reading roman numeral notation with flats
How do I lift the insulation blower into the attic?
Pre-Employment Background Check With Consent For Future Checks
Does capillary rise violate hydrostatic paradox?
Would this string work as string?
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
Mortal danger in mid-grade literature
Weird lines in Microsoft Word
Partial array output includes garbage
The Definitive C Book Guide and ListWhat is the difference between #include <filename> and #include “filename”?How do I determine the size of my array in C?How to initialize all members of an array to the same value?With arrays, why is it the case that a[5] == 5[a]?Detecting superfluous #includes in C/C++?C pointer to array/array of pointers disambiguationWhat is array decaying?C/C++ include header file orderFastest sort of fixed length 6 int arrayin a struct, scanf returns var address not value
Most of my experience is limited to SQL scripting for DBA functions. I am a security specialist and provide help to others on those topics, but I am learning C to aid in those other endeavors. I've been reading books, writing small programs, and expanding the difficulty level as I go. This is the first time I've had to reach out for help. I apologize if this has been asked, but I did search first and didn't find anything.
So far, my programs have always returned only the valid data from partially filled arrays. This particular one is not behaving the same even though I'm using the same for statement I have previously used with success. At this point I must have tunnel vision because I cannot seem to see where this is failing.
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage. It would be greatly appreciated if someone could provide some guidance on what I'm overlooking. Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct grade
int id;
int percent;
;
#define maxCount 100
int main()
int *grade;
struct grade gradeBook[maxCount];
int count = 0;
char YN;
int i;
for(i = 0; i < maxCount; i++)
void sort(struct grade gradeBook[],int cnt)
int i, j;
struct grade temp;
for (i = 0; i < (cnt - 1); i++)
for (j = (i + 1); j < cnt; j++)
if(gradeBook[j].id < gradeBook[i].id)
temp = gradeBook[j];
gradeBook[j] = gradeBook[i];
gradeBook[i] = temp;
printf("Grades entered and ordered by ID: n");
for (i = 0; i < count; i++)
printf("nID:%d, Grade: %3dn", gradeBook[i].id,gradeBook[i].percent);
return 0;
c
add a comment |
Most of my experience is limited to SQL scripting for DBA functions. I am a security specialist and provide help to others on those topics, but I am learning C to aid in those other endeavors. I've been reading books, writing small programs, and expanding the difficulty level as I go. This is the first time I've had to reach out for help. I apologize if this has been asked, but I did search first and didn't find anything.
So far, my programs have always returned only the valid data from partially filled arrays. This particular one is not behaving the same even though I'm using the same for statement I have previously used with success. At this point I must have tunnel vision because I cannot seem to see where this is failing.
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage. It would be greatly appreciated if someone could provide some guidance on what I'm overlooking. Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct grade
int id;
int percent;
;
#define maxCount 100
int main()
int *grade;
struct grade gradeBook[maxCount];
int count = 0;
char YN;
int i;
for(i = 0; i < maxCount; i++)
void sort(struct grade gradeBook[],int cnt)
int i, j;
struct grade temp;
for (i = 0; i < (cnt - 1); i++)
for (j = (i + 1); j < cnt; j++)
if(gradeBook[j].id < gradeBook[i].id)
temp = gradeBook[j];
gradeBook[j] = gradeBook[i];
gradeBook[i] = temp;
printf("Grades entered and ordered by ID: n");
for (i = 0; i < count; i++)
printf("nID:%d, Grade: %3dn", gradeBook[i].id,gradeBook[i].percent);
return 0;
c
1
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
1
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Search forcountin the code (case-sensitive).
– user3386109
Mar 7 at 0:59
add a comment |
Most of my experience is limited to SQL scripting for DBA functions. I am a security specialist and provide help to others on those topics, but I am learning C to aid in those other endeavors. I've been reading books, writing small programs, and expanding the difficulty level as I go. This is the first time I've had to reach out for help. I apologize if this has been asked, but I did search first and didn't find anything.
So far, my programs have always returned only the valid data from partially filled arrays. This particular one is not behaving the same even though I'm using the same for statement I have previously used with success. At this point I must have tunnel vision because I cannot seem to see where this is failing.
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage. It would be greatly appreciated if someone could provide some guidance on what I'm overlooking. Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct grade
int id;
int percent;
;
#define maxCount 100
int main()
int *grade;
struct grade gradeBook[maxCount];
int count = 0;
char YN;
int i;
for(i = 0; i < maxCount; i++)
void sort(struct grade gradeBook[],int cnt)
int i, j;
struct grade temp;
for (i = 0; i < (cnt - 1); i++)
for (j = (i + 1); j < cnt; j++)
if(gradeBook[j].id < gradeBook[i].id)
temp = gradeBook[j];
gradeBook[j] = gradeBook[i];
gradeBook[i] = temp;
printf("Grades entered and ordered by ID: n");
for (i = 0; i < count; i++)
printf("nID:%d, Grade: %3dn", gradeBook[i].id,gradeBook[i].percent);
return 0;
c
Most of my experience is limited to SQL scripting for DBA functions. I am a security specialist and provide help to others on those topics, but I am learning C to aid in those other endeavors. I've been reading books, writing small programs, and expanding the difficulty level as I go. This is the first time I've had to reach out for help. I apologize if this has been asked, but I did search first and didn't find anything.
So far, my programs have always returned only the valid data from partially filled arrays. This particular one is not behaving the same even though I'm using the same for statement I have previously used with success. At this point I must have tunnel vision because I cannot seem to see where this is failing.
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage. It would be greatly appreciated if someone could provide some guidance on what I'm overlooking. Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct grade
int id;
int percent;
;
#define maxCount 100
int main()
int *grade;
struct grade gradeBook[maxCount];
int count = 0;
char YN;
int i;
for(i = 0; i < maxCount; i++)
void sort(struct grade gradeBook[],int cnt)
int i, j;
struct grade temp;
for (i = 0; i < (cnt - 1); i++)
for (j = (i + 1); j < cnt; j++)
if(gradeBook[j].id < gradeBook[i].id)
temp = gradeBook[j];
gradeBook[j] = gradeBook[i];
gradeBook[i] = temp;
printf("Grades entered and ordered by ID: n");
for (i = 0; i < count; i++)
printf("nID:%d, Grade: %3dn", gradeBook[i].id,gradeBook[i].percent);
return 0;
c
c
edited Mar 8 at 3:29
Robert Durham
asked Mar 7 at 0:51
Robert DurhamRobert Durham
11
11
1
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
1
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Search forcountin the code (case-sensitive).
– user3386109
Mar 7 at 0:59
add a comment |
1
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
1
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Search forcountin the code (case-sensitive).
– user3386109
Mar 7 at 0:59
1
1
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
1
1
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Search for
count in the code (case-sensitive).– user3386109
Mar 7 at 0:59
Search for
count in the code (case-sensitive).– user3386109
Mar 7 at 0:59
add a comment |
1 Answer
1
active
oldest
votes
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage
What else did you expect?
If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.
It's really not clear what else you expected to happen here.
Perhaps loop to count the second time instead.
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's whatcountis. Which book are you using to learn C?
– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034431%2fpartial-array-output-includes-garbage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage
What else did you expect?
If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.
It's really not clear what else you expected to happen here.
Perhaps loop to count the second time instead.
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's whatcountis. Which book are you using to learn C?
– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
|
show 3 more comments
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage
What else did you expect?
If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.
It's really not clear what else you expected to happen here.
Perhaps loop to count the second time instead.
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's whatcountis. Which book are you using to learn C?
– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
|
show 3 more comments
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage
What else did you expect?
If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.
It's really not clear what else you expected to happen here.
Perhaps loop to count the second time instead.
If there are fewer than 20 inputs, the printf output displays the remaining values with garbage
What else did you expect?
If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.
It's really not clear what else you expected to happen here.
Perhaps loop to count the second time instead.
answered Mar 7 at 0:58
Lightness Races in OrbitLightness Races in Orbit
293k54477809
293k54477809
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's whatcountis. Which book are you using to learn C?
– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
|
show 3 more comments
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's whatcountis. Which book are you using to learn C?
– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements...
– Robert Durham
Mar 7 at 1:52
@RobertDurham You already did that. That's what
count is. Which book are you using to learn C?– Lightness Races in Orbit
Mar 7 at 2:26
@RobertDurham You already did that. That's what
count is. Which book are you using to learn C?– Lightness Races in Orbit
Mar 7 at 2:26
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Programming in C by Stephen Kochan was the one I have been using.
– Robert Durham
Mar 7 at 2:36
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
Cool, that's a recommended one. Good luck!
– Lightness Races in Orbit
Mar 7 at 2:37
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing...
– Robert Durham
Mar 7 at 4:02
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034431%2fpartial-array-output-includes-garbage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could the issue be in the second for-loop? It seems that you are iterating over all 20, which if you have entered less than that then the remaining would not be properly initialized and be displayed as garbage.
– brothir
Mar 7 at 0:55
Please format your code properly.
– Lightness Races in Orbit
Mar 7 at 0:56
1
Cognitive dissonance: security specialist wants to know why using uninitialized memory results in undefined behavior! :(
– paddy
Mar 7 at 0:56
Search for
countin the code (case-sensitive).– user3386109
Mar 7 at 0:59