C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-30-2001, 11:17 PM   #1
Unregistered
Guest
 
Posts: n/a
whats wrong with my code?

hello all..im in need of some assistance with my homework assignment. but before you say "oh no..", please read on. i did make an attempt! but for some reason...it is not working...so im going to post my assignment..and then show you what i came up with..please please please..tell me what im doing wrong..

Part 1.
Write a C function that deletes all occurrences of an element from an array starting
from a given index. The prototype of the function is as follows:

//Preconditions: The first parameter is an integer array; the
second parameter is an integer representing the number
of elements in the array; the third parameter is the
number that is to be deleted from the array; and the
fourth parameter is the index from which the search for
deletion should start.

//Postconditions: The function deletes all elements
whose value is equal to item starting from the index
loc. The size of the array will decrease if one or more
copies of item existed in the array. Therefore function
changes the number of elements in the array as well. The
function also returns the number of deleted items.
int delete_elemet(int a[], int *n, int item, int loc);

Part 2.
Write a program that reads a sequence of unknown number of integers into an
array. There may be a maximum of 20 integers in the input, so you should define
the size of the array to be 20. Your program should then remove duplicates of all
numbers in the array using the function that you wrote in part 1. After deleting the
duplicates, your program should also print how many copies are deleted for each
number having duplicates.
Output Specifications:

Sample Run 1:
Enter some integers (maximum of 20):
10 25 38 25 40 10 10 50 38 25 10
Value 10 : 3 copies are deleted.
Value 25 : 2 copies are deleted.
Value 38 : 1 copy is deleted.
The resulting array is:
10 25 38 40 50

Sample Run 2:
Enter some integers (maximum of 20):
21 21 21 21 21 21 21
Value 21: 6 copies are deleted.
The resulting array is:
21

Requirements:
In order to get full credits, you must write the function delete_element() as
described in part 1 and you must call it in your main function appropriately to
perform the task described in part 2.
Your main function should call the function delete_element() properly so that
it can use the value that the function returns to print the number of copies deleted
from the array. This printing must be done in the main function, not in the function
delete_element() .


ok..that was the assignment...and this is what i came up with:

#include <stdio.h>
int delete_element(int a[], int *n, int item, int loc);
int d;
int main()
{
int cnt=0, a[], *n, p, I;
while(*n<=20) {
printf(“Enter some integers (maximum of 20): \n”);
}
scanf(%d, &a[*n]);
if(cnt <=*n){
delete_element(a[], n, cnt + 1, cnt);
printf(“\n”);
if(d>1){
printf(“Value %d: %d copies are deleted.”, *n, d);
}
else{
printf(“Value %d: %d copy is deleted.”, *n, d);
p=p+d;
}
cnt= cnt + 1;
}
printf(“The resulting array is: \n”);
for(I=0; I <=p; I ++){
printf(“%d”, a[i] );
}

int delete_element(int a[], int *n, int item, int loc);
{
int x, y, d=0, l;

for(x=loc; x<n; x=y)
{
y=x;
if(a[x]==item)
{
for(l=x; l<n-1; l++) a[l]=a[l+1];
d++; n--;
} else y++;
}
return d;
}


ANY suggestions will be greatly appreciated! Thank you..
  Reply With Quote
Old 10-31-2001, 12:04 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
Well this is a fairly obvious one:
Code:
int main() 
{ 
   int cnt=0, a[], *n, p, I; 
   while(*n<=20) { 
   printf(“Enter some integers (maximum of 20): \n”); 
   }
Can you spot the two errors here? (Aside from horrible variable
naming...)

1) Notice the braces pare after the while() loop. All this would do,
assuming it actually worked, is print "enter some integers..." over
and over and over...

2) This is the real problem. You use 'n' without ever giving it a
value. This is a "BadThing(TM)". Very very bad. First off, if you
don't give a value to a pointer before using it, it points to some
random location in memory. Thus, when you derefernce it, as you
are here, you get some random result (almost always _BAD_, and
never what you expect).

3) I lied, there's a third one. Due to the way your braces are set
up, this loop will go on forever, because 'n' is never changed.
Thus, the loop will go and go and go... (or, it'll never execute,
because the random value in 'n' fails the check).

Quzah.
quzah is offline   Reply With Quote
Old 10-31-2001, 08:44 PM   #3
Registered User
 
Join Date: Sep 2001
Posts: 752
//Preconditions: The first parameter is an integer array; the
second parameter is an integer representing the number
of elements in the array; the third parameter is the
number that is to be deleted from the array; and the
fourth parameter is the index from which the search for
deletion should start.

//Postconditions: The function deletes all elements
whose value is equal to item starting from the index
loc. The size of the array will decrease if one or more
copies of item existed in the array. Therefore function
changes the number of elements in the array as well. The
function also returns the number of deleted items.
int delete_elemet(int a[], int *n, int item, int loc);

I suggest doing this with a testing program...
Code:
//Stick this somewhere in the code...
void printArray (int a[], int n)
{
 int i;
 printf ("Array: ");
 for (i = 0; i < n; i++) printf ("%d ", a[i]);
 printf ("\n");
}

// And your main could look something like this...
int main ()
{
 int array[] = {3, 4, 5, 4, 3, 3, 3, 2, 7, 4, 5};
 int count = 11;
 printf ("Count is %d.\n", count); should be 11.
 printArray (array, count); should be array

 delete_element (array, &count, 5, 0); // takes out 2 elements
 printf ("Count is %d.\n", count); should be 9.
 printArray (array, count); should be array without 5s

 delete_element (array, &count, 3, 1); // takes out 3 elements.  Leaves behind the first 3.
 printf ("Count is %d.\n", count); should be 6
 printArray (array, count); should be array without 5s or all but the first 3.

 return 0;
}

int delete_elemet(int a[], int *n, int item, int loc)
{
 // You do this part.
}
I'm having a pretty hard time with modifying the code you have however, so this is the best help I can offer. Try using the code that I have, of course inserting your own code for the delete_element function, and just kinda keep working at it untill it gets right.
__________________
Callou collei we'll code the way
Of prime numbers and pings!
QuestionC is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is wrong in this simple code vikingcarioca C Programming 4 04-23-2009 07:10 AM
what is wrong with this code please korbitz Windows Programming 3 03-05-2004 10:11 AM
I cant find what is wrong with this code senegene C Programming 1 11-12-2002 06:32 PM
Anyone see what is wrong with this code? Wise1 C Programming 2 02-13-2002 02:01 PM
very simple code, please check to see whats wrong Unregistered C Programming 3 10-10-2001 12:51 AM


All times are GMT -6. The time now is 12:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22