Take note lang bai nga dili ang sulod sa array ang gi.display kundili ang index plus 10 which is
i+10
naa na sa last part kon giunsa pag.display
ahh uu.. na.gets na nako bay. murag sakto ni imong algo. ok ra ni siya kung gamay ra ang valid numbers.. like sa gi.specify ra ts.
pero dili ni scalable. which means if muingon ang maistra nga usbon niya ang valid numbers to 1 - 1000000. syagit ni.
but for valid numbers nga up to 32767 lang, ok ra ni. mu.work ra ni. although declaring an array of size 32767 just to display, say, 10 valid inputs is probably stupid.. but mu work ra ni.
@crickwalter
bai ang iyang gpangayu is dili daw mudwat ug duplicate numbers so ang maayu buhaton mgchecking after sa input
katong imong code sa akong pagsabot imo gdwat tanang input sa user nya imo gpang remove ang duplicate. so if every input cyag 10 ka "5" ang idisplay nya usa ka 5 nlng?
mga bro!!salmat au sa inyong tabang!!! pero sad to say..la ayu ko ka gets..
low level pa jud ko..as in..level 1 pa..if ever naa level 0 naa ko dha na level..haha..!
pde ko mangayo lang ug part sa code na i chek sa program every input?
wla pd ko kabalo unsaon pag sort..haha..la me gitudluan ana..ako functions na nhblan are: printf,scanf,define,clrscr,getch,if,loops,...
agen!ty au sa pag reply!! pa help pa jud ko palihog..
Ok, pwede man di niya i.accept ang number kon dili from 10 to 50 pag.input pa lang sa user ug di xa modawat ug duplicate....depende na na sa TS kon unsaon niya pag.modify ang code...
pero pwede ing.ani...this is very far effecient compared to having the numbers sorted.
Code:int N; //the number of numbers to be inputed int numbers[41]; //there are only 41 number from 10-50 int i; //iterations //initialize the array that will hold the numbers for(i=0; i<41; i++) { numbers[i] = 0; } //Ask the user how many numbers he wants to enter printf("Please input the number of numbers you want to enter: "); //get the number scanf("%d", &N); //inform the user to input the numbers printf("Pls. enter numbers from 10 to 50"); //get the numbers for(i=0; i<N; i++) { int num=0; scanf("%d", &num); //if the number is valid flag the index corresponding to the number entered if(num>=10 && num<=50 && numbers[num-10]!=1) { numbers[num-10] = 1; } //if the number is invalid, output a message and repeat the asking of a number else { printf("Invalid or duplicate Number. Pls input another number.\n"); i--; } } //dispaly the numbers for(i=0; i<41; i++) { if(numbers[i]==1) { printf("%d\n", &i+10); } }
@DreaderX: use crickwalter's solution. it works, and it's actually relatively efficient given that the requirements are as what you described above - only numbers 10 -50 are accepted
@crickwalter: i say that your code is 'relatively' efficient because when it comes to memory allocation, it is not efficient. if the user would choose to input only 2 numbers, why would you have to allocate 51 int spaces? how much more if the constraint on the valid numbers is lifted? although, in fairness to you, your code actually works, and it will work fast for the premise that the ts gave. and, in that light, i am thinking that it is up to the ts to look for and implement the areas for improvements. he is a student after all. hehehe. oh, and yes, you are spoon feeding him.. haha
Last edited by zengatsu; 06-28-2010 at 09:53 AM.
if i understand the TS properly, one of the requirement is that the program must be able accept N number of unique integers. that means if it is set to accept 3 (unique) integers, a user can attempt to enter 6 integers but if 4 of which is just a duplicate of 2 already inputted then the program will just continue to ask for another unique number.
below is a modification of @crickwalter's code to satisfy above requirement. btw, the code below has a side effect of printing the inputted numbers in ascending order & not by the sequence they were entered by the user.
@TSCode:#include<stdio.h> main() { int N=0; //the number of numbers to be inputed int nums[51]; //51 so we could do away with -1 during assignment ;-) int i; //iterations int tmp; //temp holder for the number inputted by the user do { //Ask the user how many numbers he wants to enter printf("Please input the number of numbers you want to enter (max is 41): "); scanf("%d", &N); } while (N<=0 || N>41); //initialize the array that will hold the number to be displayed for(i=0; i<51; i++) nums[i] = 0; i=0; //reset for reuse do { //inform the user to input the numbers printf("Pls. enter a number between 10 to 50: "); scanf("%d", &tmp); if (tmp >= 10 && tmp <= 50) if (nums[tmp]) printf("Number already entered\n"); else nums[tmp] = ++i; //this value could be use to sort accdg to the sequence the numbers were entered. else printf("Number must be between 10 to 50\n"); } while (i<=N); //dispaly the numbers for(i=0; i<51; i++) if(nums[i]) printf("%d\n", i); }
ayaw lang sa kabalaka anang optimization gi mention sa mga guys. specially student pa ka. the thing to remember is, make your code run according to spec/reqs then optimize. not before![]()
Last edited by devnull; 06-26-2010 at 12:38 AM. Reason: code cleanup
Similar Threads |
|