Ok, I have tried to look online, and through my books just cant seem to find it.
A small portion of my code.
What iam trying to do is accept up to 10 numbers save it to an array, but if they input a -1 or less end the list of numbers.
I have tried a 'while' loop, rewriting the 'for' loop, iam just not getting it. Any help would be apprecited. I am also new to programing in C I have not learned about pointers yet ( saw some reference that went with array and pointers did not understand them). So a basic understanding of how to do it would be most helpful for me.
using dev C++ 4.9.9.2
Windows XP Professional
include <stdio.h>
include <stdlib.h>
define NUM 10
void bubbleSort(int pos[], int array_size);
int main()
{
int pos[NUM], i, list, nine, sum = 0, mean = 0, median = 0;
int numbers, small = 0, largest = 0, cnt;
Your loop gets exactly NUM values. You have no code to check for -1.
Show us your attempt at that. It would be better for someone to show you where you are going wrong than to simply 'do it for you'. Besides, not posting your attempt just makes it look like you are hoping for a free ride on your homework assignment.
Consider that the middle expression in the for-loop statement can be any expression; you can test both i and the input value. I suggest that you do not assign the input directly to your array, but to a separate variable, test the variable and if valid put it in the array. The loop test is then i < NUM and input valid.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just a very crazy time yesturday with that. Been trying off and on for serveral days. Did not want you to do my homework, just lost. Thank you for the time for posting.
This is what I tried after reading your post.
changing the for loop like this,
for(i = 0; i < 10 && i > -1; i++)
still only does the 10 variables.
So I wrote a while loop,
while(i <= 10 && i > -1)
{
for(i = 0; i < 10; i++) //saves the array
scanf("%d", &pos[i]); //saves the array
}
went straight into my next list of stuff that was up.
printf("1. Display the values in ascending order: \n");
scanf("%d", &list); **
without waiting for a list of numbers to be inputed.
Looked over all my notes again, plus what you said finally came together.
for(i = 0; i < NUM; ++i) //saves the array
{
scanf("%d", &temp); //saves the array
if(temp <= -1) break;
sum += temp;
pos[i] = temp;
}
count = i; //count is not my array size
Thank you for your time to post Clifford.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
> without waiting for a list of numbers to be inputed.
When you use %d with scanf, it only extracts decimal digits from the stream, that leaves the <newline> in the buffer, which then satisfies all the following scanf() calls, but without and data that satisfies the %d so the <newline> is never extracted. See http://www.it.usyd.edu.au/~dasymond/mirror/c-faq/stdio/scanfprobs.html for more information.
Your solution works, but breaking out of loops is often considered bad practice - I do for one it is a code maintenance nightmare. I would probably code it thus:
int input ;
for(i = 0; i < NUM && input != -1; i++ )
{
scanf("%d", &input);
if( input != -1)
{
sum += temp;
pos[i] = temp;
}
}
count = i - 1 ;
while( getchar() != '\n' ){ / do nothing/ } // flush line buffer
Note the necessary adjustment for count because now i is incremented before the sentinel test. Also note the method of clearing the line buffer so that any subsequent input is not aborted early.
Of course another solution is to remove the if() and write the sentinel to the array, that way you may not need to retain count at all, but your array will need to be NUM+1 long.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, I have tried to look online, and through my books just cant seem to find it.
A small portion of my code.
What iam trying to do is accept up to 10 numbers save it to an array, but if they input a -1 or less end the list of numbers.
I have tried a 'while' loop, rewriting the 'for' loop, iam just not getting it. Any help would be apprecited. I am also new to programing in C I have not learned about pointers yet ( saw some reference that went with array and pointers did not understand them). So a basic understanding of how to do it would be most helpful for me.
using dev C++ 4.9.9.2
Windows XP Professional
include <stdio.h>
include <stdlib.h>
define NUM 10
void bubbleSort(int pos[], int array_size);
int main()
{
int pos[NUM], i, list, nine, sum = 0, mean = 0, median = 0;
int numbers, small = 0, largest = 0, cnt;
system("PAUSE");
return 0;
}
//definition
void bubbleSort(int pos[], int array_size) //list 1
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (pos[j-1] > pos[j])
{
temp = pos[j-1];
pos[j-1] = pos[j];
pos[j] = temp;
}
}
}
}
Thank you once again.
Akumanight
Your loop gets exactly NUM values. You have no code to check for -1.
Show us your attempt at that. It would be better for someone to show you where you are going wrong than to simply 'do it for you'. Besides, not posting your attempt just makes it look like you are hoping for a free ride on your homework assignment.
Consider that the middle expression in the for-loop statement can be any expression; you can test both i and the input value. I suggest that you do not assign the input directly to your array, but to a separate variable, test the variable and if valid put it in the array. The loop test is then i < NUM and input valid.
Clifford
Sorry Clifford,
Just a very crazy time yesturday with that. Been trying off and on for serveral days. Did not want you to do my homework, just lost. Thank you for the time for posting.
This is what I tried after reading your post.
changing the for loop like this,
for(i = 0; i < 10 && i > -1; i++)
still only does the 10 variables.
So I wrote a while loop,
while(i <= 10 && i > -1)
{
for(i = 0; i < 10; i++) //saves the array
scanf("%d", &pos[i]); //saves the array
}
went straight into my next list of stuff that was up.
printf("1. Display the values in ascending order: \n");
scanf("%d", &list); **
without waiting for a list of numbers to be inputed.
Looked over all my notes again, plus what you said finally came together.
for(i = 0; i < NUM; ++i) //saves the array
{
scanf("%d", &temp); //saves the array
if(temp <= -1) break;
sum += temp;
pos[i] = temp;
}
count = i; //count is not my array size
Thank you for your time to post Clifford.
> for(i = 0; i < 10 && i > -1; i++)
You are testing the index, not the input.
> without waiting for a list of numbers to be inputed.
When you use %d with scanf, it only extracts decimal digits from the stream, that leaves the <newline> in the buffer, which then satisfies all the following scanf() calls, but without and data that satisfies the %d so the <newline> is never extracted. See http://www.it.usyd.edu.au/~dasymond/mirror/c-faq/stdio/scanfprobs.html for more information.
Your solution works, but breaking out of loops is often considered bad practice - I do for one it is a code maintenance nightmare. I would probably code it thus:
int input ;
for(i = 0; i < NUM && input != -1; i++ )
{
scanf("%d", &input);
if( input != -1)
{
sum += temp;
pos[i] = temp;
}
}
count = i - 1 ;
while( getchar() != '\n' ){ / do nothing/ } // flush line buffer
Note the necessary adjustment for count because now i is incremented before the sentinel test. Also note the method of clearing the line buffer so that any subsequent input is not aborted early.
Of course another solution is to remove the if() and write the sentinel to the array, that way you may not need to retain count at all, but your array will need to be NUM+1 long.
Clifford