PDA

View Full Version : C programming


Dr_s99
11-20-05, 07:31 PM
hello,
:P
I need hellp with this question!
its a easy question except my brain has stopped working after the math mid-term! :p

The Question is:
Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

Thank you,

supra
11-20-05, 10:29 PM
i havent programmed C in a long long time now but maybe something like this?


int num[19];
int x;

void main()
{
for (x = 0; x < 20; x++)
{
printf("please enter value for %d : ", i);
scanf("%d", &num[x]);

if (num[x] < 10 || num[x] > 100)
{
printf("value must be between 10-100");
printf("please enter value for %d : ", i);
scanf("%d", &num[x]);
}
else
{
//for loop to go through num array to check for duplicates
//soz, too lazy to code the rest lol
}

}
}

Zelda_fan
11-20-05, 11:09 PM
so basically you are asking us to do your homework for you. lol

Greg
11-21-05, 03:54 AM
so basically you are asking us to do your homework for you. lol
Who's suggesting to give the right answer ;) Some forums actually have a homework policy.

Dr_s99
11-21-05, 07:42 AM
lol well not really :P or maybe i am :S
i can't see how the loop is for checking to see if a number is duplicate or not!

Subtestube
11-21-05, 12:31 PM
My assumption is that supra just means to do a complete check of everything in the array for each number that's read in. All you do in that for loop is check the current input against all the others already in... If you find a duplicate, just set a flag saying 'don't print', and then when you get to the end of the main for loop, print only when the don't print boolean is false, and then re-set it.

Also, bear in mind that Supra's solution uses a size 19 array. That's fine, but you must not try to read the 20th number into the array if you're going to do that, as you'll be out of bounds. You'll need to keep it in an integer (which isn't saving any space in any case). Although numbering starts at 0, a size 19 array will start at 0 and finish at 18 - you won't be able to store the 20th number in it.

EDIT: Also, you shouldn't come here trying to get people to do homework for you - it's not a good look ;|. I'll give advice on how to code a solution, but I won't give you code. (If this were a genuine code problem like "I'm making a graphics thing, how do I get a window in the screen using the GLUT, I'd be much more willing to help).

wnd
11-21-05, 01:19 PM
If you really want to pack the stuff in an array as small as possible concider not wasting one bit per number (as numbers [10, 100] = [0, 90] can be presented with seven bits). This way it is trivial to spend only 17 octets (or 8-bit bytes) for storing those 19 numbers you have to.

IMO your assignment would have made more sense (while still being trivial) if the input would've been large (like 10^9 numbers) and you would be to optimize performance instead.

seeker010
11-22-05, 11:27 AM
take only what you need
malloc()...
free()
:D

Subtestube
11-22-05, 12:14 PM
Hah! True - nice work wnd. That's a clever way to use no more space than required, though I doubt that that's what's required of them.

Dr_s99
11-22-05, 02:49 PM
nvm i finnished it myself!!
thank you!

PETAPAL
07-18-11, 02:23 AM
#include <stdio.h>
#define MAX 20

int main()
{
int a[ MAX ] = { 0 }; /* user input */
int i; /* counter */
int j; /* counter */
int k = 0; /* number of integers entered */
int duplicate; /* notify of duplicates */
int value;

printf( "Enter 20 numbers between 10 - 100;\n" );

/* ask user for 20 numbers */
for ( i = 0; i <= MAX - 1; i++ ){

duplicate = 0;
scanf( "%d", &value);

/* decide if integer is duplicate */
for ( j = 0; j < k; j++ ) {

/* notify and stop loop if duplicate */
if ( value == a[ j ] ) {
duplicate = 1;
break;
{ /* end if */



/* enter number into array if it's not a duplicate */
if ( !duplicate )

a[ k++ ] = value;

} /* end if */

ViN86
07-18-11, 01:26 PM
I doubt the OP still has this problem or even visits the forums anymore, but I have moved the thread to the proper section.

Addison_16
07-22-11, 07:45 AM
#include <stdio.h>
int main()
{
int a[ MAX ] = { 0 };
int i, j,k = 0;
int duplicate;
int value;
printf( "Enter 20 numbers between 10 - 100;\n" );
for ( i = 0; i <= MAX - 1; i++ ){
duplicate = 0;
scanf( "%d", &value);
for ( j = 0; j < k; j++ ) {
if ( value == a[ j ] ) {
duplicate = 1;
break;
{



/* enter number into array if it's not a duplicate */
if ( !duplicate )

a[ k++ ] = value;

} /* end if */