|
|
#1 | |
|
geForce 4 Ti 4600
Join Date: Sep 2003
Location: Canada
Posts: 339
|
hello,
:P I need hellp with this question! its a easy question except my brain has stopped working after the math mid-term! 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,
__________________
System info(s): ----- Gaming/Work System : ASUS G1 RAM: 2.0Gb Hard drive: 160 Gb VIDEO CARD: NVIDIA 7700Go 512Mb OS: Windows Vista Home Pre. ---------------------------- Server/Work system: CPU: AMD Athlon XP 2400+ RAM: 1.0Gb Hard drive: 30 IDE,120 IDE VIDEO CARD: Nvidia Geforce 4 Ti 4600 OS: OpenSuse 10.2, Windows 2003 |
|
|
|
|
|
#2 | |
|
2JZ powered
|
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 } } }
__________________
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: Aug 2004
Posts: 10,299
|
so basically you are asking us to do your homework for you. lol
|
|
|
|
|
#4 | |
|
Registered User
Join Date: Aug 2002
Location: Australia
Posts: 1,177
|
Quote:
Some forums actually have a homework policy. |
|
|
|
|
|
#5 |
|
geForce 4 Ti 4600
Join Date: Sep 2003
Location: Canada
Posts: 339
|
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!
__________________
System info(s): ----- Gaming/Work System : ASUS G1 RAM: 2.0Gb Hard drive: 160 Gb VIDEO CARD: NVIDIA 7700Go 512Mb OS: Windows Vista Home Pre. ---------------------------- Server/Work system: CPU: AMD Athlon XP 2400+ RAM: 1.0Gb Hard drive: 30 IDE,120 IDE VIDEO CARD: Nvidia Geforce 4 Ti 4600 OS: OpenSuse 10.2, Windows 2003 |
|
|
|
|
#6 | |
|
Anisymbolic
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
|
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).
__________________
Dr Possible: Core 2 Duo E6400 on Gigabyte GA-965P-DS4. Galaxy GeForce 7600GT. 2GB Corsair XMS 2 DDR2-6400 RAM (CL5). ATi Theatre 550 Pro. Windows XP MCE. All stored in Piano black Antec Sonata II, with a broken door. Mobile: ASUS M2400N, Pentium M 1.5 GHz. 512 MB DDR RAM. Intel EXTREME graphics. Windows XP SP 2 / Ubuntu 5.10. Ridiculous DOES not have an 'e' in it. It comes from "ridicule" and has less than nothing to do with the colour red. |
|
|
|
|
|
#7 |
|
Nerd, Geek, Freak
Join Date: Sep 2005
Location: Finland
Posts: 703
|
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.
__________________
web | cat Christianity, noun: The belief that a cosmic Jewish Zombie who was his own father can make you live forever if you symbolically eat his flesh and telepathically tell him you accept him as your master, so he can remove an evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree. [mad.frog] |
|
|
|
|
#8 |
|
Registered User
Join Date: Sep 2003
Posts: 112
|
take only what you need
malloc()... free() ![]() |
|
|
|
|
#9 |
|
Anisymbolic
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
|
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 Possible: Core 2 Duo E6400 on Gigabyte GA-965P-DS4. Galaxy GeForce 7600GT. 2GB Corsair XMS 2 DDR2-6400 RAM (CL5). ATi Theatre 550 Pro. Windows XP MCE. All stored in Piano black Antec Sonata II, with a broken door. Mobile: ASUS M2400N, Pentium M 1.5 GHz. 512 MB DDR RAM. Intel EXTREME graphics. Windows XP SP 2 / Ubuntu 5.10. Ridiculous DOES not have an 'e' in it. It comes from "ridicule" and has less than nothing to do with the colour red. |
|
|
|
|
#10 |
|
geForce 4 Ti 4600
Join Date: Sep 2003
Location: Canada
Posts: 339
|
nvm i finnished it myself!!
thank you!
__________________
System info(s): ----- Gaming/Work System : ASUS G1 RAM: 2.0Gb Hard drive: 160 Gb VIDEO CARD: NVIDIA 7700Go 512Mb OS: Windows Vista Home Pre. ---------------------------- Server/Work system: CPU: AMD Athlon XP 2400+ RAM: 1.0Gb Hard drive: 30 IDE,120 IDE VIDEO CARD: Nvidia Geforce 4 Ti 4600 OS: OpenSuse 10.2, Windows 2003 |
|
|
|
|
#11 |
|
Registered User
Join Date: Jul 2011
Posts: 1
|
#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 */ |
|
|
|
|
#12 |
|
Registered User
Join Date: Mar 2004
Posts: 15,486
|
I doubt the OP still has this problem or even visits the forums anymore, but I have moved the thread to the proper section.
|
|
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Video: New Accelerations for Parallel Programming | News | Latest Tech And Game Headlines | 0 | 06-25-12 07:20 AM |
| Google Blockly: New Visual Programming Language | News | Latest Tech And Game Headlines | 0 | 06-13-12 06:00 AM |
| Video: Programming Heterogeneous Many-cores Using Directives | News | Latest Tech And Game Headlines | 0 | 05-23-12 08:00 AM |
| nCore Schedules Popular Multicore Programming Course for Houston | News | Latest Tech And Game Headlines | 0 | 05-14-12 05:00 PM |
| Slidecast: Scalable HPC ' New Accelerations for Parallel Programming Languages over | News | Latest Tech And Game Headlines | 0 | 05-08-12 07:10 AM |