I need to create a program that adds two sets,
hint: the set is composed of distinct (integer)


How to do this using this Function:
void add(int result[],int item,int *count);
add result the value of the item.

Please i really need ur immediate help...pls600x

Recommended Answers

All 9 Replies

I need to create a program that adds two sets,
hint: the set is composed of distinct (integer)


How to do this using this Function:
void add(int result[],int item,int *count);
add result the value of the item.

Please i really need ur immediate help...pls600x

Well, here's my immediate help:
Can you show us the code you already have?

my code is this :

#include<stdio.h>
#define SIZE 3

void add (int result[],int item,int *count)
{
int i;


for(i=0; i<SIZE; i++)
{
scanf("%d",&result[i]);
if (result[i]==item)
 {
printf("&d",result[i]);
(*count)++;
  }
}


}










main()

{
int result[3],item,*count=0;

clrscr();

add(result,item,&count);


getch();

}

please correct my code,and i also want the user to input the sets,like enter 1st set and second set then add the two sets...please..thnks

This is already a big error: int result[3],item,[B]*count=0;[/B] .

You declare a pointer to an integer, correct till here, but what do you do then?
Well, you assign a value to it, hmm, what could be wrong here?
Remember this for ever: do not assign a value to a pointer which you didn't explicitly assign a memory address to.
In your program you're just writing the value zero to a random location in your computer's memory, but that's not what you want, am I right?
So, how to fix this?
Well, before assigning the value zero to that pointer, you let the pointer point to the desired memory address first.

Why int result[3] ?? At the top of your program's code you defined a constant for it: SIZE, this isn't a bug or something, but to prevent potential bugs it would be better to change it to: int result[SIZE] Integer variable item will have a random value as well, because you didn't initialize this variable.

You probably meant this whole statement: int result[3],item,*count=0; like this: int result[3],item[B]=0[/B],*count[B]=result[/B]; What's the point of that if-statement and that scanf function in this loop?

for(i=0; i<SIZE; i++)
{
  scanf("%d",&result[i]);
[B]  if (result[i]==item) // (1)
  {[/B]
    printf("&d",result[i]);
  [B]  (*count)++; // (2)[/B]
[B]  }[/B]
}

(1) why this if??
(2) increasing the value where the pointer points to with one??
(this isn't the same as moving the pointer to the next element in the array, if you meant that, you should have written it like this: count++; )

Could you please post using code tags?

:)

hello thanks for the reply,hmm..could u please change the logic of the program coz i want that the user wil input the value to be added in the set. PLease and kindly correct it.

#include<stdio.h>
#define SIZE 3

void add (int result[],int item,int *count)
{
int i;


for(i=0; i<SIZE; i++)
{
scanf("%d",&result[i]);
if (result[i]==item)
 {
printf("&d",result[i]);
(*count)++;
  }
}


}


main()

{
int result[3],item=0,*count=0;

clrscr();

add(result,item,&count);

getch();

}

hello thanks for the reply,hmm..could u please change the logic of the program coz i want that the user wil input the value to be added in the set. PLease and kindly correct it.

I have put much effort in writing such an extensive post, so could you please try first? (look at my previous post)

Edit::
If I didn't misunderstand you, your assignment was: Get some values from the user, store them in an array, get another value from the user, and increase each value in the array with that value.
Am I right on this?

Little correction on tux's post.
tux>This is already a big error: int result[3],item,*count=0; .
You declare a pointer to an integer, correct till here, but what do you do then? Well, you assign a value to it, hmm, what could be wrong here?

Assign 0 or NULL to the pointer variable.
There is no problem with this declaration.

int *count=0;  /*  or  int *count=NULL */

This is invalid.

int *t;
    *t=0;

krish4th,

Remove the pointer declaration from the main().

int main() {
    int result[3],item=0,count=0;
    ..
    return 0;
  }
commented: Uhm, yes, you're right :) +10

@adatapost : There is a huge difference between just 0 and NULL . You may not get an error but that doesn't mean you are right. ASCII 0 is null character value even using '\0' is better when using null character rather than just a 0 and even in that condition too the compiler doesn't give an error but this is better.

But using 0 for NULL (null pointer) is inviting trouble at some point.NULL (null pointer) is defined in a standard way as (void *)0.So...

null character proper usage '\0'
null pointer correct usage NULL or (void *) 0

rather than just a 0 for both.

Even though just a 0 is correct instead of NULL when used with pointers its better to maintain the distinction between integer 0,null character,null pointer and finally ASCII value 0

I referred not only the above links but also 7.11,7.17,6.3.2.3 etc of www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf before posting the above code. And I advocated for the standards defined and most of the posts say 0 was bought in later and may be problematic in some situations,so you need to know when to use 0 and when you are compelled to use (void*)0 zero specifically.So its at least better to know how NULL is actually defined.

commented: Nice link to the standard :) +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.