How do you replace duplicate elements? How do you prompt the user to replace that specific array address?Code:#include <stdio.h> int main() { int array[10]={1,2,2,3,4,4,5,6,7,7} ... }
How do you replace duplicate elements? How do you prompt the user to replace that specific array address?Code:#include <stdio.h> int main() { int array[10]={1,2,2,3,4,4,5,6,7,7} ... }
Herd Mastodon with me:
https://linuxrocks.online/invite/ov3SKzQY
What would you like to replace them with? Will the array always be sequential?
3.14159265358979323846264338327950288
4197169399375105820974944592307816406
28620899862803482534211706798 pi 101
So lets say the array was assembled from user input. How do you prune the array during the loading process, when the user is actually keying in the data?
Where I am at right now:
Code:/*Program contains an array of size 20. Allow user to load array. Ensure no duplication. Use at least 2 functions to achieve goal. Display content of array with location.*/ #include <stdio.h> #define SIZE 20 int main(void) { int array[SIZE]; int c; for (c=0; c<SIZE; c++) { printf("Enter an integer:\n"); /* something here to check if input is same as previous*/ scanf("%i",&array[c]); } printf("%s\t\t%s\n","Array subscript","Value"); for(c=0; c<SIZE; c++) { printf("%8d\t\t%2d\n",c,array[c]); } }
Last edited by carey; Mar 19, 2011 at 12:29 AM. Reason: merged multiple posts
Herd Mastodon with me:
https://linuxrocks.online/invite/ov3SKzQY
The %i in scanf should be %d.
You'd really want to check after scanf as well since that's where you read the number.
Instead of a for loop you could use a while loop and only proceed if the number read is not equal to array[i-1]. You might therefore want to use a temporary variable to hold the number and check it before assigning it to the array. Also, watch out for index 0 since there is no index at -1
3.14159265358979323846264338327950288
4197169399375105820974944592307816406
28620899862803482534211706798 pi 101
These are identical according to documents.
Doesn't scanf assign the value to the variable right away?You'd really want to check after scanf as well since that's where you read the number.
We are instructed that the 'for loop' is the same mechanism, but cleaner because it's a closed function(italicized words mine)Instead of a for loop you could use a while loop and only proceed if the number read is not equal to array[i-1].So the idea is to store the first input into a variable, pass it to the array, then compare every other input afterwards?You might therefore want to use a temporary variable to hold the number and check it before assigning it to the array. Also, watch out for index 0 since there is no index at -1
Herd Mastodon with me:
https://linuxrocks.online/invite/ov3SKzQY
I was wondering about that, didn't check. Cool
Yes it does. I was saying that the comment /* something here to check if input is same as previous*/ (or code that should replace it) should be after you've read the variable.
They can both be written to behave the same way. What I was suggesting was the removal of the increment on every iteration, since if it is repeated you might not want to move down the array.
Store the first in the array, then for the others, store them in a variable and compare with the previous index before inserting them.
3.14159265358979323846264338327950288
4197169399375105820974944592307816406
28620899862803482534211706798 pi 101
Still stuck. I want to:
1. watch for duplicates in the array
2. prompt the user to replace the duplicate
Herd Mastodon with me:
https://linuxrocks.online/invite/ov3SKzQY
Code:#include <stdio.h> #include <conio.h> #define SIZE 10; void function(int); int array[SIZE],c=0,x,val; int main() { for (c=0; c<SIZE; c++) { printf("Enter an integer:\n"); scanf("%d",&val); function(val); } printf("%s\t\t%s\n","Array subscript","Value"); for(c=0; c<SIZE; c++) { printf("%8d\t\t%2d\n",c,array[c]); } getch(); return 0; } void function(int val) { for(x=0;x<SIZE;x++) /* something here to check if input is same as previous*/ { if (array[x]==val) { printf("enter a new value"); scanf("%i",&array[x]); } } }
Last edited by wiel; Mar 20, 2011 at 11:58 AM. Reason: globalize!!!
1337
That seems like it!
I adapted it a bit.
I think I better post my entire source code:
So far it doesn't run.Code:/*Program contains an array of size 20. Allow user to load array. Ensure no duplication. Use at least 2 functions to achieve goal. Display content of array with location.*/ #include <stdio.h> #define SIZE 20 //this is the array size originally void table_head(void);// for output table int dup_check();// duplication checker int array[SIZE]; int main(void) { int c;// for incrementing int check,newone; //for (c=0; c<SIZE; c++) c=0; while (c<SIZE) { printf("Enter an integer:\n"); if (c==0) { scanf("%i",*array);//first entry not duplicate continue; } scanf("%i",&check); dup_check(); array[c]=newone; c++; } table_head(); for(c=0; c<SIZE; c++) { printf("%8d\t\t%2d\n",c,array[c]); } } void table_head(void) { printf("%s\t\t%s\n","Array subscript","Value"); } int dup_check(int check)//Thanks to wiel on Techjamaica.com forums { int x; int newone; for(x=0; x<SIZE; x++) /* something here to check if input is same as previous*/ { if (array[x]==check) { printf("You already entered %i.\nEnter another integer:\n",check); scanf("%i",&newone); return newone; } else array[x]=check; } }
Herd Mastodon with me:
https://linuxrocks.online/invite/ov3SKzQY