Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// Here, taking an array of 10 elements and assign some values at random positions.
// Now i want to know that on which location some data has been entered( suppose i don't know //what data and where user has given some input) and which location is having garbage value.what //can be the logic..? One more thing, user has given i/p 0 to at some location.

#include< stdio.h >
int main()
{
int arr[10];
arr[1]=2;
arr[4]=8;
arr[6]=0;
arr[7]=6;
.....
.....
..... // what can be the logic..?
.....
}
Posted
Comments
Richard MacCutchan 4-Oct-13 3:41am    
Initialise all the elements of the array to some fixed value when you create it.

If your int array has not been specifically initialised it will probably contain random values. There is no test for "garbage".

So the steps are:
(1) Initialise your array to known values that are not also valid input:

C++
const int initval = -9999;
for (int i=0; i < 10; i++)
   arr[i] = initval;

(2) After data has been entered go through the array and see which values have been changed. You may do something like this.

C++
bool validValues[10];

for (int i=0; i < 10; i++)
{
   arr[i] = initval;
   validValues[i] = false;
}

// After data entry

for (int i=0; i < 10; i++)
{
   if (arr[i] != initval)
      validValues[i] = true;
}


There may be more elegant ways but the principle is the same.
 
Share this answer
 
v10
If you want to find out garbage values (or non-garbage values), first, you must define what is a garbage value.

Eg:- If your array will only have unsigned values then you can use a negative value (-1, -2, -3....) as your garbage value.

C++
int arr[10];
int garbage = -1;

//initialize your array
for(int i=0; i < 10; i++)
{
   arr[i] = garbage;
}

//now test for garbage
for(int i=0; i < 10; i++)
{
   if(arr[i] == garbage)
   {
      //do something with this piece of garbage
   }
   else
   {
      //it's not garbage and do something else
   }
}


Alright pal, now this method works fine, but what if I can't use any value in the range of int as the garbage value ?

Well, for this case, the following approach is suitable. Just read the following code, you'll understand the mechanism.

C++
typedef struct {
   int value;
   bool is_used; //if this is true then the 'value' is not garbage.
}MyArray;

MyArray arr[10];

//initialize your array
for(int i=0; i < 10; i++)
{
   arr[i].value = 0;
   arr[i].is_used = false;
}

//test for garbage values
for(int i=0; i < 10; i++)
{
   if(arr[i].is_used == true)
   {
      //not garbage, do something
   }
   else
   {
      //garbage, do something else
   }
}


The first approach if more efficient when you know the range of your valid input values, if not the second method is more clear, simple and understandable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900