How to delete row number using C programing? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a Structure, in the StructureI have an Array,
I read a text file and then open it in `Array Into Structure ',
What I have is a list of names, Last, results.
so what is the best way to find a row number and select which row to delete and delete it? I said, Array into a Structure.?
I know I can use memmove and realloc but how do I use these?

Well, all you can do is move the following elements towards the start, and decrease the "logical" length. The logical length is different from the physical length, which is the maximum number of elements the array can hold, based on how much memory has been allocated.
So, assuming an array starting at array and with count elements, code to delete the n:th element would be:
if( n < count - 1)
memmove(array + n, array + n + 1, ((count - n) - 1) * sizeof *array);
--count;
This copies the following elements (unless you're deleting the very last one, in which case there's nothing to copy) and then decreases the logical length.

Related

Problems with arrays in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given a shifted array (for example):
[17 34 190 1 4]
which shifted from (we don't know original)
[1 4 17 34 190]
What would be a good function to find the position of that number?
For example if I pass 1, it would return 3th position.
linear search for answer would always work, but I believe you can get there in O(log) time.
Some sort of binary search for the shift point via checking if the value of the shift sorted array goes against what it is supposed it. Like creating a trie. Keep forming the sorted tree until you find the "illegal" node (man this is glossing over a lot of details - I know). That tells you where the inflection point is and you now treat the array as 2 sorted vectors. Quickly check to see if the value to find is larger than the max entry of each so we know which vector to search. BSearch the sorted vector for your value and return its index.
The hard part is finding the inflection point. :)
You would have to scan the array.
size_t pos_in_arr(int *arr, size_t arr_size, int match)
{
size_t i;
for (i = 0; i < arr_size; i++)
if (arr[i] == match)
break;
return i;
}
This function would return the position as asked, or one more than the maximum position in case the element is not found.
The solution is what you ask, but it is probably not what you need, because it does not use in any way the fact that the array has been shifted. I suspect the original problem to be more complex.
For example if you knew that in the original array one element was fifth and now is seventh, and the element you are looking for was twenty-third, you could answer "twenty-fifth" without actually scanning the array up to the twenty-fifth position, which could be the point of the whole exercise. But to build such a solution, one would need to know more about the problem.

How to get the distinct count of a column using C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to get the distinct count of the column of a large data file using C.How can I do it.Please kindly advise me.Thanks.My sample data file is as below.
For 2nd attribute the distinct count is 6.
399547,v4149,p3178,1990,2065,fraud
399940,v5852,p3194,8278,2180,fraud
399983,v3476,p3199,766,1125,fraud
400206,v3467,p3216,494,311000,fraud
400345,v4497,p3219,1211,432100,fraud
400471,v3473,p3225,41392,3710,fraud
400498,v3476,p3225,102,23820,fraud
401325,v4497,p3297,1322,1110,fraud
Make a search tree for every column. Let's say you have 10 rows in a file with 2 distinct values for the nth column viz. 3456 and 3457. Your search tree for nth column will look like:
You'll end up with 6 Search trees. Once you have read the entire file, traverse all possible paths in each search tree and that will give you the number of distinct values.
Read and split every line.
Put the second attributes into an array.
qsort the array
You have now an array with equal strings adjacent to each other. You can loop over the array and count different entries.
If your entries are all 5 characters long, otherwise you must malloc() memory for each attribute.
char (*array)[6];
int i;
int n; /* number of lines read */
int distinct = 1;
/* read the data file and put it into array */
/* qsort() array */
for (i = 1; i < n; ++i) {
if (strcmp(array[i], array[i - 1]) != 0)
++distinct;
}
printf("There are %d distinct rows\n", distinct);
You can use std::map<std::string,int> - it will hold key-value pairs, where key is vNNNN, and value is number of repetitions.
First loop will scan input file and populate this map, then number of keys in map will be distinct count.
EDIT: If you cannot use C++ and do require C, you will have to find some hashmap library for C, like sparsehash.
If amount of data is really, really big, it is possible that it will not fit in memory. In this case, I would recommend to use SQLite temporary database to parse, store and index your data and then use standard SELECT DISTINCT on it.

Search for a specific character in a 2D array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering how you would search through a 2D array (used as a parameter for a function), and find a specific character, e.g. an exclamation mark?
Say I have a 2D array island[20][40] and I want to find the character X. My approach would be to use a nested for loop, to go through each element and an if statement. E.g.
for (i = 0; i < 20; i++) {
for (j = 0; j < 40; j++) {
//Not sure what goes here (I want a function that identifies the element in the array)
if ((some variable) == 88)
printf("The treasure is at: (%d, %d)", i, j);
Thanks for your help :)
-island[20][40] works fine. I just want to know how to search through it for a specific character.
use the condition
if (island[i][j] == 88);
If your array(s) are not ordered then you have no choice but to search through sequentially, there are no short cut.

Using of arrays in "C" printing array value with out using braces [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have the following array:
int array[]={0,1,2,3,4};
I need to print the element 3 in the array list without using square brackets. So I should not use:
printf("%d",array[3]);
How do I achieve the same without using square brackets?
array[n] is equivalent to *(array + n).
You can just use *(array + 3). This is different syntactically to array[3] but identical functionally.
The expression array + 3 gives you the address of the fourth element in the array (index number 3), properly scaled for the element size. In other words, it's the same as taking the address of the element with &(array[3]).
Then the * dereferencing extracts the value at that address (of the correct type).
To print the nth element , we know arr[n-1] == *(arr+n-1) . n-1 is being used because array in C are 0-indexed.

What is the best answer for finding the maximum sum possible in an array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The question is :
Find the maximum sum possible in an array of positive integers by selecting the elements in such a way that no two elements are next to each other.
there is an answer like this :
but what is the best answer for this question
Let's denote the array by "t" and index it from 0. Let f be a
function so that
f(k)=the maximal sum in the [0..k] subarray with the conditions of the problem.
Now use dynamic programming:
f(0) = t[0]
f(1) = max{ t[0], t[1] }
f(k) = max{ f(k-2) + t[k], f(k-1) } if k >= 2
If the array has n elements we need f(n-1).
Thanks in advance.
Solution you proposed is good one.
Similar approach (page 7 here):
Let m[i] be the maximum sum of any subarray that ends at the element a[i].Then
m[i] is simply max(a[i], m[i-1]+a[i]).
This is O(n).
and you cant get anything below O(n) as you have to visit every item of the array atleast once to compute the result.
Well, I think this is already the best answer.
Since you need O(n) to read in the data.
an O(n) algorithm is the fastest in the big-O notation.
public static int maxSum(int[] A){
return maxSum(A,0,1);
}
private static int maxSum(int[] A, int x, int y){
int c =0, d=0;
if(x<A.length){
c = A[x]+maxSum(A,x+2,x+3);
}
if(y<A.length){
d = A[y]+maxSum(A,y+2,y+3);
}
return c>d?c:d;
}

Resources