Problems with arrays in C [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 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.

Related

Correct my thinking in this C exercise [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 9 years ago.
The exercise asks to find which of the numbers from 1 to 500, the sum of the numbers specific digits, raised to the third power equals that particular number.
for example 1^3=1
and 371 makes 3^3+7^3+1^3 = 371
How I approached the problem:
I was thinking if I could have an array of strings with 500 slots, each slot containing a string converted number, then I could do math with each slot's string. If they met the criteria I would apply then that slot would be printed.
I tried the function sprintf without much success. In a loop it just initializes the strings (or is it arrays? after 3 hours I am confused) [0] slot, leaving all other slots unchanged.
I don't want you to solve the exercise, rather than guide me with my logic. Please ask me to add code of what I did if you want to.
Always start by clearly defining your algorithm, so you know what you are doing. Split it up into simple steps. Something like this:
For each integer i in the interval 1 to 500:
Check if the condition holds for this i
If it holds:
Print i
else:
Do nothing
Now you need to define "Check if the condition holds for this i". I would use some modulo and division arithmetics to extract the digits, but I leave the details to you.
Note that I have talked nothing about C or any other programming language. Only when you know your algorithm should you start thinking about implementation.
(There is actually the possibility of a slightly different algorithm than the one given above, where you have one loop for each digit nested inside each other. That solution may be acceptable to you but it will not be as generic)
for(i=1;i<=500;i++)
{
//loop for checking each number i
int sum=0; // to store the sum of cube of digits
int n=i; //copy of i
//The below while loops does the task. It extracts a digit from the number and adds its cube to the sum
// last digit from the number can be seen by taking its remainder by 10 . For eg 35%10=5
//once we have used this digit make the number shorter by dividing by 10. For eg 35/10 becomes 3 (because of integer divisions)
while(n>0)
{
int rem=n%10; //extract the last digit
sum+=cube(rem); //cube function raises a number to its cube
n/=10; //remove the digit we had extracted earlier from the number
}
if(sum==i) //we got the number we wanted
printf("%d\n",i);
}

How to delete row number using C programing? [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 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.

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.

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