Random numbers being printed instead of my var
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a exercise and then I need to read as many numbers as the user wants, show the higher one and how many times it was read. I tried to build the code and in my mind it should work, but when it comes to show the number and etc it shows some "random numbers" instead of my var called "max".
#include <stdio.h>
#include <stdlib.h>
int main()
{
int aux1, j, i,k, max = -99999,cont;
printf("How many numbers will be read: t");
scanf("%d", &aux1);
int vet[aux1];
for(k=0;k<aux1;k++){
printf("Digit a number: t");
scanf("%d",&vet[k]);
}
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
cont=1;
j=0;
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
printf(" The higher number is %d and it was read %d times n", max, cont);
system("pause");
return 0;
}
If i input on screen that i want to read 3 numbers as a example, then input 1, 2 and 3 it gives me that the higher number is "1954048954" and was read 1 times, when it should give me "3 is the higher number and it was read 1 time"
c
add a comment |
I have a exercise and then I need to read as many numbers as the user wants, show the higher one and how many times it was read. I tried to build the code and in my mind it should work, but when it comes to show the number and etc it shows some "random numbers" instead of my var called "max".
#include <stdio.h>
#include <stdlib.h>
int main()
{
int aux1, j, i,k, max = -99999,cont;
printf("How many numbers will be read: t");
scanf("%d", &aux1);
int vet[aux1];
for(k=0;k<aux1;k++){
printf("Digit a number: t");
scanf("%d",&vet[k]);
}
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
cont=1;
j=0;
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
printf(" The higher number is %d and it was read %d times n", max, cont);
system("pause");
return 0;
}
If i input on screen that i want to read 3 numbers as a example, then input 1, 2 and 3 it gives me that the higher number is "1954048954" and was read 1 times, when it should give me "3 is the higher number and it was read 1 time"
c
2
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
1
You should always check the return value ofscanf
– Gerhardh
Jan 3 at 13:27
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14
add a comment |
I have a exercise and then I need to read as many numbers as the user wants, show the higher one and how many times it was read. I tried to build the code and in my mind it should work, but when it comes to show the number and etc it shows some "random numbers" instead of my var called "max".
#include <stdio.h>
#include <stdlib.h>
int main()
{
int aux1, j, i,k, max = -99999,cont;
printf("How many numbers will be read: t");
scanf("%d", &aux1);
int vet[aux1];
for(k=0;k<aux1;k++){
printf("Digit a number: t");
scanf("%d",&vet[k]);
}
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
cont=1;
j=0;
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
printf(" The higher number is %d and it was read %d times n", max, cont);
system("pause");
return 0;
}
If i input on screen that i want to read 3 numbers as a example, then input 1, 2 and 3 it gives me that the higher number is "1954048954" and was read 1 times, when it should give me "3 is the higher number and it was read 1 time"
c
I have a exercise and then I need to read as many numbers as the user wants, show the higher one and how many times it was read. I tried to build the code and in my mind it should work, but when it comes to show the number and etc it shows some "random numbers" instead of my var called "max".
#include <stdio.h>
#include <stdlib.h>
int main()
{
int aux1, j, i,k, max = -99999,cont;
printf("How many numbers will be read: t");
scanf("%d", &aux1);
int vet[aux1];
for(k=0;k<aux1;k++){
printf("Digit a number: t");
scanf("%d",&vet[k]);
}
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
cont=1;
j=0;
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
printf(" The higher number is %d and it was read %d times n", max, cont);
system("pause");
return 0;
}
If i input on screen that i want to read 3 numbers as a example, then input 1, 2 and 3 it gives me that the higher number is "1954048954" and was read 1 times, when it should give me "3 is the higher number and it was read 1 time"
c
c
edited Jan 3 at 13:14
dbush
104k14110146
104k14110146
asked Jan 3 at 13:08


ErikErik
153
153
2
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
1
You should always check the return value ofscanf
– Gerhardh
Jan 3 at 13:27
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14
add a comment |
2
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
1
You should always check the return value ofscanf
– Gerhardh
Jan 3 at 13:27
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14
2
2
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
1
1
You should always check the return value of
scanf
– Gerhardh
Jan 3 at 13:27
You should always check the return value of
scanf
– Gerhardh
Jan 3 at 13:27
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14
add a comment |
1 Answer
1
active
oldest
votes
You're reading past the end of your array:
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
If you enter in 3 for aux1
, then vet
is an array of 3 numbers, but here you loop through 10 array elements. This reads past the end of the array, invoking undefined beahvior.
Fix the loop to be within the size of the array:
for(i = 0 ; i<aux1 ; i++){
if(vet[i] > max) max=vet[i];
}
You have a similar issue here when counting the max element:
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
You have an off-by-one error here, your loop condition should instead be:
while(j<aux1){
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
add a comment |
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%2f54022943%2frandom-numbers-being-printed-instead-of-my-var%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
You're reading past the end of your array:
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
If you enter in 3 for aux1
, then vet
is an array of 3 numbers, but here you loop through 10 array elements. This reads past the end of the array, invoking undefined beahvior.
Fix the loop to be within the size of the array:
for(i = 0 ; i<aux1 ; i++){
if(vet[i] > max) max=vet[i];
}
You have a similar issue here when counting the max element:
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
You have an off-by-one error here, your loop condition should instead be:
while(j<aux1){
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
add a comment |
You're reading past the end of your array:
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
If you enter in 3 for aux1
, then vet
is an array of 3 numbers, but here you loop through 10 array elements. This reads past the end of the array, invoking undefined beahvior.
Fix the loop to be within the size of the array:
for(i = 0 ; i<aux1 ; i++){
if(vet[i] > max) max=vet[i];
}
You have a similar issue here when counting the max element:
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
You have an off-by-one error here, your loop condition should instead be:
while(j<aux1){
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
add a comment |
You're reading past the end of your array:
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
If you enter in 3 for aux1
, then vet
is an array of 3 numbers, but here you loop through 10 array elements. This reads past the end of the array, invoking undefined beahvior.
Fix the loop to be within the size of the array:
for(i = 0 ; i<aux1 ; i++){
if(vet[i] > max) max=vet[i];
}
You have a similar issue here when counting the max element:
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
You have an off-by-one error here, your loop condition should instead be:
while(j<aux1){
You're reading past the end of your array:
for(i = 0 ; i<10 ; i++){
if(vet[i] > max) max=vet[i];
}
If you enter in 3 for aux1
, then vet
is an array of 3 numbers, but here you loop through 10 array elements. This reads past the end of the array, invoking undefined beahvior.
Fix the loop to be within the size of the array:
for(i = 0 ; i<aux1 ; i++){
if(vet[i] > max) max=vet[i];
}
You have a similar issue here when counting the max element:
while(j<=aux1){
if(max==vet[j]){
cont++;
}
j++;
}
You have an off-by-one error here, your loop condition should instead be:
while(j<aux1){
answered Jan 3 at 13:10
dbushdbush
104k14110146
104k14110146
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
add a comment |
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
omg, thanks a lot.
– Erik
Jan 3 at 13:13
omg, thanks a lot.
– Erik
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
@Erik glad I could help. Feel free to accept this answer if you found it useful.
– dbush
Jan 3 at 13:13
add a comment |
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%2f54022943%2frandom-numbers-being-printed-instead-of-my-var%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
2
Your line "for(i = 0 ; i<10 ; i++)" should probably be "for(i = 0 ; i<aux1 ; i++)"
– cprlkleg
Jan 3 at 13:12
Learn how to debug small programs: use a debugger.
– Sourav Ghosh
Jan 3 at 13:13
1
You should always check the return value of
scanf
– Gerhardh
Jan 3 at 13:27
Note that you don't actually have to read all the numbers in memory at once. You can keep track of the max and how many times it's been read as you read them.
– Jani
Jan 3 at 13:31
Yeah, that's true. I'll try to change it later, thanks to everyone.
– Erik
Jan 3 at 15:14