How can I overwrite a word from an 2d array? - c

I have created a 2d array where a user can input words.
I need to create 2 functions:
1st function is called Add and an Admin can add a user and his data.
2nd function is called Delete and an Admin can remove a user by typing his username. If his username is found in the 2d array created in Add function then all of user's data should be replaced by zeros. The thing is that I cannot overwrite/replace the user's data to zero.
The 2d array:
char pin[50][7][100];
each row contains a single user and each column contains his info like name, surname etc.
Here is the delete function:
printf("Enter the username of the user you want to delete: \n");
scanf("%s",&key);
for(i=0;i<50;i++){
for(j=5;j<6;j++){
if (strcmp(pin[i][j],key)==0){
k=i;
flag=1;
break;
}
}}
if (flag==1){
do{
printf("Are you sure you want to delete this user?\t (Yes or No)\n");
scanf("%s",&api);
}while((strcmp(api,"Yes")!=0) && (strcmp(api,"No")!=0) );
if (strcmp(api,"Yes")==0){
/* Here I need to replace with 0s!*/
}
else if (strcmp(api,"No")==0) {
goto dlt;
}
}
else{
printf("Error!Username not found.Please try again. \n");
}

I presume that the column 5 contains the username, and you have found the user you want to delete at row k.
You can delete the data by using strcpy to replace all the data with 0s.
for (j = 0; j < 7; j++)
{
strcpy(pin[k][j],"0");
}

Related

Problem with deleting record from linear list (C)

At first, I would say sorry for my English, but i hope you will understand me and I need realy fast answer...
I am coding my project in C to school, and I have some trouble with deleting a record from my linear list.
I am reading the records from the text file, and I write it to a linear linked list. I will not post here the whole program, just function "vstup_z".
These records are something like a database of houses and flats (their price, city, expanse etc). And this function is scanning the string, and it should delete the whole particular record, when the scanned string is a substring of the item presenting the name of city. For example when I have in my records some flat from London and I will call this function in the program (for example "z Lon"), this function will delete this record.
Here is some code of my function:
void vstup_z(REALITY **p_first)
{
REALITY *p_act = NULL, *p_help = NULL;
char s[51] = "", arr_city[51] = "", city[51] = "";
int counter = 0, i = 0;
getchar();
gets(arr_city);
// this function is working even with upper or lower case, it doesnt matter
for (i = 0; i < strlen(arr_city); i++)
{
arr_city[i] = tolower(arr_city[i]);
}
p_act = *p_first;
while (p_act != NULL)
{
strcpy(city, p_act->miesto_ponuky); //miesto_ponuky is item in my record in slovak language
for (i = 0; i < strlen(city); i++)
{
city[i] = tolower(city[i]);
}
if ((strstr(city, arr_city)) != NULL)
{
if (p_act != *p_first)
{
p_help->p_next = p_act->p_next; //one of the items in my record (structure) is pointer on next record
free(p_act);
p_act = p_help;
}
else
{
*p_first = p_act->p_next;
free(p_act);
p_act = *p_first;
}
counter++; // how many records did i deleted
}
p_help = p_act;
if (p_act != NULL)
{
p_act = p_act->p_next;
}
}
printf("Vymazalo sa %d zaznamov\n", counter); // just some text in slovak language how many records i deleted...
}
In my next function (which is working 100% right) i am writing this linked list on the display...
What's my problem ?
General info about my issue:
I can't delete all records.
When i have for example 1 record in list I can delete it - it works.
When I have 5 records in list, I can delete 4 of them, but never all of them, and it is a problem...
When I have 5 records, and 3 of them have an item with name of the city, for example London, and the next two have an item with the name of city Washington, and I call this function in program like "z o" - it should delete every record whose item presenting the name of city contains character "o"; it means it should delete all of them. But it will always save the last one (but when my list consists of just one record, it will delete it right).
Sorry for my bad English. I hope you understand; if not, never mind...
Any solutions ?
regarding:
while (p_act != NULL)
...
p_help = p_act;
if (p_act != NULL)
{
p_act = p_act->p_next;
}
the statement: p_act = p_act->p_next; causes p_act to point to NULL, however, there is still an entry in the list (pointed to by p_help) to be checked (and possibly deleted)

C Table implemented by an array

Basically I'm trying to implement a table with help of an array. The array functionality is already given and should not be altered. What I'm having trouble configuring is my table_remove function. Keep in mind that I'm not striving for the most effective way to do this.
What I tried to do is loop through the entire table to find if there is a key that matches.
If the key is found, save the position.
If not exit.
So after the position is found I set it to free the key & value on that position in hope that it will 'remove' the key & value pair from that position. However, my given test program returns "Removing the last element from a table does not result in an empty table.". If I add in the end "array_setVal(array, NULL, index) then the entire program crashes (Probably because it tries to write on that null position further on in the test).
So now I'm wondering if I'm approaching this issue the wrong way and have to do further operations to actually remove the value from the position without sort of messing up the position itself so next time I use table_insert, the position will be empty and another key & value pair will be inserted in that spot.
int saveRemovedIndex = -1;
/* Check if the key already exists */
for(int indexCounter = 0; indexCounter <= MAXINDEX; indexCounter++){
i = array_inspectValue(t->values, indexCounter);
if (t->cf(i->key,key)==0) {
saveRemovedIndex = indexCounter;
break;
}
}
/* Checks if the key actually exists in the table.
* If it exists, remove it. Else quit.*/
if (saveRemovedIndex != -1) {
i = array_inspectValue(t->values, saveRemovedIndex);
if(t->keyFree!=NULL) {
t->keyFree(i->key);
}
if(t->valueFree!=NULL) {
t->valueFree(i->value);
}
} else {
return;
}

ImageJ get value from an array

I am trying to match image files to a specific row in an array.
In a first step I am selecting a folder that contain the tif files.
Then I am selecting a csv file with the information I would like to use.
I am opening each image one after the other. For an opened image I would like to retrieve the values from the line that correspond to the image name in the csv file. The header of the CSV file is FileName, XValue, YValue.
Here's my code so far... Any help would be appreciated.
// Make a pop up to select the input directory
InputDirPath=getDirectory("Select Input directory");
// Get the list of files in the input input directory selected above as an array
InputFileList=getFileList(InputDirPath);
// Defines cell separator and line separator
CellSeparator=",";
LineSeparator="\n";
// open the csv file as a string
FileCCValuesPath=File.openDialog("Select the file containing the coordinates");
FileCCValuesString=File.openAsString(FileCCValuesPath);
// Split each row into an array
FileCCValuesRows=split(FileCCValuesString, LineSeparator);
// Create new arrays for the content of column and for each row
FileNameArray=newArray(FileCCValuesRows.length);
XValueArray=newArray(FileCCValuesRows.length);
YValueArray =newArray(FileCCValuesRows.length);
// Start of the loop going through the list of image files in the input folder selected above
for (Filei = 0; Filei < InputFileList.length; Filei++)
{
InputFileNamei=InputFileList[Filei];
InputFilePathi = InputDirPath+InputFileNamei;
if(endsWith(InputFilePathi, ".tif"))
{
open(InputFilePathi);
//////////This is where I am stuck
//////////Get the XValue and Value from the CSV file for the row in which
//////////FileName=InputFileNamei
run("Translate...", "x=XValue y=YValue interpolation=None");
}//end if
}//end for File i loop
// Notice of end of process
waitForUser("Process is done");
You can add another for loop (after opening the current image) that goes through all lines and checks if the line starts with the current image name and then split the current line to get the x and y values:
for (i=0; i < FileCCValuesRows.length; i++) {
if (startsWith(FileCCValuesRows[i], InputFileNamei)) {
values = split(FileCCValuesRows[i], CellSeparator);
xValue = parseInt(values[1]);
yValue = parseInt(values[2]);
run("Translate...", "x=" + xValue+ " y=" + yValue + " interpolation=None");
}
}
Note that the "ImageJ" way of doing it would be to open your CSV file in a Results table and use the getResultString and getResult macro functions to get the required values. Here's a version of your macro using these:
// Make a pop up to select the input directory
InputDirPath=getDirectory("Select Input directory");
// Get the list of files in the input input directory selected above as an array
InputFileList=getFileList(InputDirPath);
// open the csv file in a Results table
FileCCValuesPath=File.openDialog("Select the file containing the coordinates");
open(FileCCValuesPath);
// Start of the loop going through the list of image files in the input folder selected above
for (Filei = 0; Filei < InputFileList.length; Filei++)
{
InputFileNamei=InputFileList[Filei];
InputFilePathi = InputDirPath+InputFileNamei;
if(endsWith(InputFilePathi, ".tif"))
{
open(InputFilePathi);
for (i=0; i < nResults; i++) {
if (getResultString("FileName", i) == InputFileNamei) {
xValue = getResult("XValue", i);
yValue = getResult("YValue", i);
run("Translate...", "x=" + xValue+ " y=" + yValue + " interpolation=None");
}
}
}//end if
}//end for File i loop
// Notice of end of process
waitForUser("Process is done");
Thank you very much for your replies
This is what I end up doing:
1-Split the array by row using
FileValuesRows=split(FileValuesString, LineSeparator);
2- Create new array for each column (I removed one line because of the header)
Column1Array=newArray(FileValuesRows.length-1);
Column2Array=newArray(FileValuesRows.length-1);
3- Create a for loop that screen each row and break it into each individual column (Starting at 1 because of the header)
for (Rowi=1;Rowi<FileCCValuesRows.length; Rowi++){
FileCCValuesRowi=split(FileCCValuesRows[Rowi], CellSeparator);
//Array.show(FileCCValuesRowi);
4- Add the content content of each row into the previously created array (-1 because of the header)
Column1Array[Rowi-1]=FileCCValuesRowi[0];
Column2Array[Rowi-1]=FileCCValuesRowi[1];
}
//end if for Rowi
5- In the next step the aim is to find the row number corresponding to current open image. This is done in two steps:
5.1- Screen the csv file for the number of occurrence of the string (in this case the filename)
5.2 if occurrence is non null add them to an Indices Array
5.3 Use this indice to get the value corresponding to that row in the array created before
//Returns the indices at which a value occurs within an array
Occurence=0;
// Screen the FileName Array row by row and count the number of occurence
for (Rowi=0; Rowi<lengthOf(FileNameArray); Rowi++) {
if (FileNameArray[Rowi]==InputFileName) {
Occurence++;
//print(Occurence);
} //end of if
} // end of for Rowi
// If found
if (Occurence>0) {
// Create an array of length the number of occurence
IndicesArray=newArray(Occurence);
Occurence=0;
// Screen the FileName Array row by row and add the row of occurence into the Indices Array
for (Rowi=0; Rowi<lengthOf(FileNameArray); Rowi++) {
if (FileNameArray[Rowi]==InputFileName) {
IndicesArray[Occurence]=Rowi;
Occurence++;
}
}
//Array.show(IndicesArray);
}
Final step
//Get the X and Y translation value for the File being processed
In this case I have only 1 occurrence so I will take the first line of the Indice array which is the line 0
XValue=Column1Array[(IndicesArray[0])];
YValue=Column2Array[IndicesArray[0]];
//Translate the picture
run("Translate...", "x=XValue y=YValue interpolation=None");

Search element in array

I'm creating an Address Book program using array. I've done with the add and print data option. But now I'm stuck with the search/update option.
This is my code in searching for the element if it exist in my array or not.
public void update_data(){
String user_input_data;
int search_data = 0;
System.out.println("Enter the data that you want to search: ");
user_input_data = user_data.nextLine();
while(search_data<data_recorded){
if(user_input_data == AddressBook_Array_name[search_data])
{
System.out.println("Data found!");
}
else
{
System.out.println("Data not found!");
}
search_data++;
}
}
But when I run the program. It always return to false and print the else statement.
I don't know what's wrong with it. Anyway the data_recorded variable holds the number of data inputted by the user in the add option.
You need to use equals() instead of == in java for comparision.
if (user_input_data.equals(AddressBook_Array_name[search_data]))
Also, instead of the while you may want to use the foreach loop (removes the need for search_data variable).
for(String addressBookElem : AddressBook_Array_name) {
if (user_input_data.equals(addressBookElem)) {
System.out.println("Data found!");
return;
}
}
System.out.println("Data not found!"); // reaches this statement if data not present
i think you should use .equals function instead of ==.

Delete records using integer list

I want to Loop through the array of integers and want to remove the items in the TLToProcess list which i have stored in the array of integers
here is the code
I want to remove only the selected in the list integer
iSize.add(TLToProcess.size());
if(TLToProcess[i].Scan_In1__c==null)
{
if(TLToProcess[i].typew__c=='Pending')
{
TLForMissingHHhh.add(TLToProcess[i]);
}
}
else if ( c[i].Scan_In1__c!=null)
{
if (TLToProcess[i].typew__c=='Pending' )
{
TLToProcess[i].typew__c='Processed';
}
}
}
Now i want to remove record 1 by 1 from TLToProcess using
remove() can any body tell me how to do it.
Thanks
Anu
Not sure I understand your problem, but if what you're trying to avoid is modifying your List of integers inside a loop and getting this error: {"Collection was modified; enumeration operation may not execute."} you can create a copy of your List(.ToList()) and use it to iterate, and this way you can call Remove() safely.
List<Int32> arr = new List<Int32>();
for (int i = 0; i < 10; i++)
{
arr.Add(i);
}
foreach(var o in arr.ToList())
{
arr.Remove(o);
}
Is that the intent?

Resources