Search for a specific character in a 2D array [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.
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.

Related

a complicate example using loop invariant [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.
can anyone supply a complicate example using loop invariant example such as sum(int n) is so trivial that it can not show the power of loop invariant. I want a example that is not that obivious, and we can use method like loop invariant to solve it.
The Wikipedia example is quite good:
for (int i = 0; i < n; i++) {
x = y + z;
a[i] = 6 * i + x * x;
}
Two invariant can be moved (y + z and x * x). The advantage of this example is that after LICM has been applied, you can apply other optimizations on the code to have something very easy.
There are plenty on papers/slides/courses about that, you sure can find a satisfying example.

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.

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;
}

Multiplication table 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.
Tips on how to do multiplication table in c??
ummm... two (nested) loops?
This should work. Really.
printf("2x1=2");
printf("2x2=4");
printf("2x3=6");
printf("2x4=8");
...
You should read books this is very basic things of programming you must clear this things yourself.
I personally recommend you not just to post here and get answer
Try reading and try developing by yourself before you post it overhere.
http://www.cprogramming.com/tutorial/c/lesson3.html
int main () {
int n = 10;
int i, j;
// Print row labels
for (i=1; i<=n; i++) {
for (j=1; j<=n; j++) {
//printf("\t%d",i*j);
//Do something here to get it working.. :-)
}
printf("\n");
}
}
You have to use 2 nested loops and if you want to make it more organized, use a two-dimensional array.

Resources