nV News Forums


nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   C programming (http://www.nvnews.net/vbulletin/showthread.php?t=60250)

Dr_s99 11-20-05 07:31 PM

C programming
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

Re: C programming
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]);
//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

Re: C programming
so basically you are asking us to do your homework for you. lol

Greg 11-21-05 03:54 AM

Re: C programming

Originally Posted by Zelda_fan
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

Re: C programming
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

Re: C programming
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

Re: C programming
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

Re: C programming
take only what you need

Subtestube 11-22-05 12:14 PM

Re: C programming
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

Re: C programming
nvm i finnished it myself!!
thank you!

PETAPAL 07-18-11 02:23 AM

Re: C programming
#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;
{ /* 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

Re: C programming
I doubt the OP still has this problem or even visits the forums anymore, but I have moved the thread to the proper section.

All times are GMT -5. The time now is 02:36 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Copyright ©1998 - 2014, nV News.