The function should simply read a matrix.
Why does it freeze after I enter the first character?
#include "stdafx.h"
#include <iostream>
using namespace std;
void as(char **p,int n,int m)
{
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
{
cout << "p[" << i << "][" << j << "]=";
cin >> p[i][j];
}
}
int main()
{
char *a[100];
as(a, 3, 3);
return 0;
}
This is undefined behavior: your array is an array of 100 pointers to char. But you've never initialized them. So when you address p[i] it gets an uninitialized pointer that could point anywhere, and when you dereference it with p[i][j] you might then freeze or suffer of anyother symptom of undefined behavior.
If you want to learn to use pointers and arrays:
Solution 1: define your array as char a[100][100];
Solution 2: in the outer loop of as(), start to allocate the chars with p[i] = new char[m];
If you want to learn modern C++:
Solution 3: Forget about memory allocation and deallocation and use vectors instead. The vectors are totally dynamic, so no maximum of 100 rows anymore:
void as(vector<vector<char>> &p, int n, int m)
{
p.resize(n);
int i, j;
for (i = 0; i < n; i++) {
p[i].resize(m);
for (j = 0; j < m; j++)
{
cout << "p[" << i << "][" << j << "]=";
cin >> p[i][j];
}
}
}
int main()
{
vector<vector<char>>a;
as(a, 3, 3);
return 0;
}
If you want to try online...
Solution 4: you want modern C++, but you'd like to use your the elements in a[] as they were a string, for easy output and manipulation, just use the same code as above but replace vector<vector<char>> with vector<string>
And here you can look online the slightly simplified code.
I have for you simple pseudo array on mallocs reallocs and pointers. Maybe it will be interesting for you:
typedef struct arr_str_t{
size_t rows, columns;
char **table;
}dynamicStringTable_t;
int CreateStringTable(dynamicStringTable_t **ptr, int rows, int columns)
{
int result = 0;
*ptr = (dynamicStringTable_t *)malloc(sizeof(dynamicStringTable_t));
if (ptr == NULL) return - 1;
(*ptr)->rows = rows;
(*ptr)->columns = columns;
(*ptr) -> table = (char *)malloc(rows * columns * sizeof(char *));
if (*ptr == NULL)
{
free(*ptr);
return -1;
}
for (int i = 0; i < rows * columns; i++) (*ptr)->table[i] = NULL;
return 0;
}
char *getString(dynamicStringTable_t *ptr, int x, int y)
{
char *result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) ? NULL : "";
if (result != NULL)
{
result = ptr->table[x + y * ptr->rows];
}
return result;
}
int putString(dynamicStringTable_t *ptr, int x, int y, const char *str)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || str == NULL || !x || !y) * -1;
if (!result)
{
char *tmp = (char *)realloc(ptr->table[x + y * ptr->rows], (strlen(str) + 1) * sizeof(char));
if (tmp == NULL) result = -2;
else
{
ptr->table[x + y * ptr->rows] = tmp;
strcpy(tmp, str);
}
}
return result;
}
int removeString(dynamicStringTable_t *ptr, int x, int y)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;
if (!result)
{
free(ptr->table[x + y * ptr->rows]);
ptr->table[x + y * ptr->rows] = NULL;
}
return result;
}
int destroyStringTable(dynamicStringTable_t *ptr, int x, int y)
{
int result = (ptr == NULL || x >= ptr->columns || y >= ptr->rows || !x || !y) * -1;
if (!result)
{
if (ptr->table != NULL)
{
for (int i = ptr->rows * ptr->columns - 1; i >= 0; i--)
free(ptr->table[i]);
free(ptr->table);
}
free(ptr);
}
return result;
}
You have a great problem in your code. You are facing a UB in:
char *a[100]; // an array of 100 pointer to a character
// it is not initialized yet
Above no element is initialized (even not allocated).
To correct your code:
char *a[100];
// Allocating the array of 100 elements on the heap:
for(int i(0); i < 100; i++){
a[i] = new char[100]; // let's say your array is n = m
}
as(a, 3, 3);
for(int i = 0; i < 3; i++){
for(int j(0); j < 3; j++)
cout << a[i][j] << ", ";
cout << endl;
}
Last but not least Don't forget to free up memory when you are done with the dynamic array:
for(int i = 0; i < 100; i++)
delete[] a[i];
Related
I am trying to solve a problem that it want the program to output the result of n^84601. (n=0,1,...,10)
Therefore, I try to solve it by using big integer, and it works well in small number, but segfault in bigger ones.
#include <stdlib.h>
#include <iostream>
using namespace std;
const int MX = 100000;
struct BigInt {
int ar[MX];
int len;
BigInt(int n) {
int i = 0;
while (n != 0) {
ar[i] = n % 10;
n /= 10;
i++;
}
len = i;
}
BigInt times(BigInt x) {
BigInt tmp(0);
for (int i = 0; i < len; i++) {
for (int j = 0; j < x.len; j++) {
int r = ar[i] * x.ar[j] + tmp.ar[i + j];
tmp.ar[i + j] = r % 10;
tmp.ar[i + j + 1] += r / 10;
}
}
for (int i = min(len + x.len, MX - 1);; i--) {
if (tmp.ar[i] != 0) {
tmp.len = i + 1;
break;
}
}
return tmp;
}
void print() {
for (int i = len - 1; i >= 0; i--) {
cout << ar[i];
}
cout << endl;
}
};
BigInt poww(BigInt a, int n) {
if (n == 1) {
return a;
}
BigInt x = poww(a, n / 2);
BigInt y = x.times(x);
if (n % 2 == 1) {
y = y.times(a);
}
return y;
}
int main(void) {
ios::sync_with_stdio(false);
int n;
while (cin >> n) {
if (n == 0)
cout << 0 << endl;
else if (n == 1)
cout << 1 << endl;
else
poww(BigInt(n), 86401).print();
}
return 0;
}
When I change the MX in to 10000 and 86401 into 864, it can correctly caculate 2^864. But it will segfault with 2^86401.
You have a stack overflow.
Your BigInt object is quite large: it contains 100001 ints, which is usually 400,004 bytes.
You allocate several of these on the stack (some are unnecessary: you should really pass arguments by const reference).
You have recursion.
A typical stack size limit is 8MB.
Combine above statements together, and you can see that you can have at most 20 BigInts on the stack at one time. Your recursion depth is at least 17, so creating more than one BigInt on the stack for each recursive call is guaranteed to fail.
There are a few solutions:
use more efficient encoding -- currently you are using int to hold one digit, unsigned char would be more appropriate
allocate space for digits on heap instead of on the stack. If you do that, be aware of the rule of five.
#include <cstdlib>
#include <iostream>
#include <Math.h>
#include <algorithm>
#include <string>
#include <iterator>
#include <iostream>
#include <vector> // std::vector
using namespace std;
int stepCount, i, x, y, z, j, k, array1Size, array2Size, tester, checker;
int numstring[10] = { 0,1,2,3,4,5,6,7,8,9 };
int numstringTest[10] = { 0,1,2,3,4,5,6,7,7,9 };
int* numbers;
int* differentNumbers;
int* p;
int* otherNumbers;
void stepCounter(int a) {
// determines the step number of the number
if (a / 10 == 0)
stepCount = 1;
else if (a / 100 == 0)
stepCount = 2;
else if (a / 1000 == 0)
stepCount = 3;
else if (a / 10000 == 0)
stepCount = 4;
else if (a / 100000 == 0)
stepCount = 5;
else if (a / 1000000 == 0)
stepCount = 6;
else if (a / 10000000 == 0)
stepCount = 7;
else if (a / 100000000 == 0)
stepCount = 8;
else if (a / 1000000000 == 0)
stepCount = 9;
}
void stepIndicator(int b) {
// indicates each step of the number and pass them into array 'number'
stepCounter(b);
numbers = new int[stepCount];
for (i = stepCount; i>0; i--) {
//
/*
x = (round(pow(10,stepCount+1-i)));
y = (round(pow(10,stepCount-i)));
z = (round(pow(10,stepCount-i)));
*/
x = (int)(pow(10, stepCount + 1 - i) + 0.5);
y = (int)(pow(10, stepCount - i) + 0.5);
numbers[i - 1] = (b%x - b%y) / y;
}
}
int sameNumberCheck(int *array, int arraySize) {
//checks if the array has two or more of same integer inside return 1 if same numbers exist, 0 if not
for (i = 0; i<arraySize - 1; i++) {
//
for (j = i + 1; j<arraySize; j++) {
//
if (array[i] == array[j]) {
//
return 1;
}
}
}
return 0;
}
void getDifferentNumbers(int* array, int arraySize) {
//
k = 0;
j = 0;
checker = 0;
otherNumbers = new int[10 - arraySize]; //exact number of other numbers is 10 - numbers we have
for (i = 0; i<10; i++) {
if ((i>0)&(checker = 0)) {
k++;
otherNumbers[k - 1] = i - 1;
}
//
checker = 0;
for (j = 0; j<arraySize; j++) {
//
p = array + j;
cout << *p << endl; //ilkinde doğru sonra yanlış yapıyor?!
if (*p = i) {
checker++;
}
}
}
}
int main(int argc, char *argv[])
{
stepCounter(999999);
cout << stepCount << endl;
stepIndicator(826424563);
for (j = 0; j<9; j++) {
//
cout << numbers[j] << endl;
}
cout << sameNumberCheck(numstringTest, 10) << " must be 1" << endl;
cout << sameNumberCheck(numstring, 10) << " must be 0" << endl;
cout << endl;
getDifferentNumbers(numstringTest, 10);
cout << endl;
cout << endl << otherNumbers[0] << " is the diff number" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Hi, my problem is with pointers actually. You will see above, function getDifferentNumbers. It simply does a comparement if in any given array there are repeated numbers(0-9). To do that, I passed a pointer to the function. I simply do the comparement via pointer. However, there is a strange thing here. When I execute, first time it does correct, but secon time it goes completely mad! This is the function:
void getDifferentNumbers(int* array, int arraySize) {
//
k = 0;
j = 0;
checker = 0;
otherNumbers = new int[10 - arraySize]; //exact number of other numbers is 10 - numbers we have
for (i = 0; i<10; i++) {
if ((i>0)&(checker = 0)) {
k++;
otherNumbers[k - 1] = i - 1;
}
//
checker = 0;
for (j = 0; j<arraySize; j++) {
//
p = array + j;
cout << *p << endl; //ilkinde doğru sonra yanlış yapıyor?!
if (*p = i) {
checker++;
}
}
}
}
and this is the array I passed into the function:
int numstringTest[10] = {0,1,2,3,4,5,6,7,7,9};
it should give the number 7 in otherNumbers[0], however it does not. And I do not know why. I really can not see any wrong statement or operation here. When I execute, it first outputs the correct values of
numstringTest: 1,2,3,4,5,6,7,7,9
but on next 9 iteration of for loop it outputs:
000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888
You have some basic problems in your code.
There are multiple comparisons that are not really comparisons, they're assignments. See the following:
if((i>0) & (checker=0)){
and
if(*p = i){
In both cases you're assigning values to the variables, not comparing them. An equality comparison should use ==, not a single =. Example:
if (checker == 0) {
Besides that, you're using & (bitwise AND) instead of && (logical AND), which are completely different things. You most likely want && in your if statement.
I've just noticed this:
getDifferentNumbers(numstringTest, 10);
and in that function:
otherNumbers = new int[10 - arraySize];
which doesn't seem right.
I am trying to implement the function stoi() in c++. I have made an int array arr to store the integer ASCII of all elements of char_arr. This works fine if I print the values from my char_arr array because its a character array. But, how do I transfer my integer values from the char array to an int array and print only the numbers and not their ASCII?
Code:
int stoi(){
int *arr = new int [strlen(char_arr)];
for (int i=0; char_arr[i]!='\0'; ++i){
arr[i] = char_arr[i];
}
for (int i=0; char_arr[i] != '\0'; ++i){
if (arr[i] >= 48 && arr[i] <= 57){
cout << char_arr[i];
}
}
}
First of all, remove the first loop and use char_arr directly. You don't need to hold ints to make it work.
As for printing int values, you can use this:
for (int i = 0; char_arr[i] != '\0'; ++i) {
if (char_arr[i] >= '0' && char_arr[i] <= '9') { //I would suggest you to use this syntax instead of raw ASCII codes.
cout << (char_arr[i] - '0');
}
}
int stoi(){
/* if you do not use arr.
int *arr = new int[strlen(char_arr)];
for (int i = 0; char_arr[i] != '\0'; ++i){
arr[i] = char_arr[i];
}
*/
int sign = 1, value = 0;
if (*char_arr == '+') {
++char_arr;
}
else if (*char_arr == '-') {
++char_arr;
sign = -1;
}
while (*char_arr) {
if (*char_arr >= '0' && *char_arr <= '9') {
value = value * 10 + *char_arr - '0';
++char_arr;
} else {
break;
}
}
return sign * value;
}
Here's the one I came up with:
#include <cstdio>
#include <cstring>
#define _BASE_ 10
int main(int argc, char **argv)
{
char ascii[] = "474927";
signed int value = 0;
signed int ascii_len = strlen(ascii);
int pos = 0;
for(signed int i = ascii_len-1; i >= 0; i--)
{
if(i == 0 && ascii[i] == '-')
{
value *= -1;
continue;
}
int base = 1;
if(pos > 0)
{
base = _BASE_;
for(int j = 1; j < pos; j++)
base *= _BASE_;
}
value += base * (ascii[i] - 48);
pos++;
}
printf("Value: %d\n", value);
return 0;
}
My code is below. The problem happens when I try and run the addArray() function. I am completely new to C++ so I have no idea what a segmentation fault means.
I also know that there is probably a better way to initialize and return the 2d arrays, but I am slowly figuring that out.
My main problem now is the segmentation fault. I am guessing that it has something to do with how I am accessing the variables?
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;
int c, q, w, row, coll, quit, qq, opt;
int** arr1;
int** arr2;
int** ans;
//Method Prototypes
int menu();
inline int** getArray(int opt);
inline void printArray(int** arr, int height, int width);
void addArray();
void subtractArray();
void multiplyArrays();
void determArray();
void transposeArray();
void inverseArray();
//Prints out the menu for choosing which option to go with
int menu() {
cout << "Press 1 for Addition\n";
cout << "Press 2 for Subtraction\n";
cout << "Press 3 for Multiplication\n";
cout << "Press 4 for Determinant\n";
cout << "Press 5 for Transpose\n";
cout << "Press 6 for Inverse\n";
cout << "Press 0 to quit\n\n";
cin >> c;
return c;
}
//Main method
int main(void) {
cout << "C++ 2d Matrix Operations Menu\n";
c = menu();
while (c != 0) {
if (c == 1) {
addArray();
} else if (c == 2) {
subtractArray();
} else if (c == 3) {
void multiplyArrays();
} else if (c == 4) {
void determArray();
} else if (c == 5) {
void transposeArray();
} else if (c == 6) {
}
c = menu();
}
cout << "Press Enter to Quit. GOOD BYE";
cin >> quit;
return 0;
}
/*
Prints out the specified array.
It's arguments are the actual array and the height/weight
*/
inline void printArray(int** arr, int height, int width) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::cout << arr[i][j] << ' ';
}
std::cout << std::endl;
}
}
//Returns an array.
inline int** getArray(int opt) {
if (opt == 0) {
cout << "How many rows and columns should be in the array?\n";
cin >> q >> w;
} else {
q = 3;
w = 3;
}
int** ary = new int*[q];
for (int i = 0; i < q; ++i) {
ary[i] = new int[w];
}
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
cout << "What should the value be for item" << row << "," << coll << "\n";
cin >> ary[row][coll];
}
}
return ary;
}
//Adds arrays
void addArray() {
arr1 = getArray(0);
int h1 = q;
int w1 = w;
arr2 = getArray(0);
int h2 = q;
int w2 = w;
if ((h1 != h2) || (w1 != w2)) {
cout << "Both arrays must be the same size.";
return;
}
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[row][coll] + arr2[row][coll];
}
}
printArray(ans, q, w);
}
//Subtracts Arrays
void subtractArray() {
arr1 = getArray(0);
int h1 = q;
int w1 = w;
arr2 = getArray(0);
int h2 = q;
int w2 = w;
if ((h1 != h2) || (w1 != w2)) {
cout << "Both arrays must be the same size.";
return;
}
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr2[row][coll] - arr1[row][coll];
}
}
printArray(ans, q, w);
}
//Calculate the determinate of an array.
void determArray() {
arr1 = getArray(1);
printArray(arr1, q, w);
//There must be a better/more efficient way to do this using loops.
int determinant = arr1[0][0]*((arr1[1][1] * arr1[2][2]) - (arr1[2][1] * arr1[1][2])) - arr1[0][1]*(arr1[1][0] * arr1[2][2] - arr1[2][0] * arr1[1][2]) + arr1[0][2]*(arr1[1][0] * arr1[2][1] - arr1[2][0] * arr1[1][1]);
printf("\nDeterminant of vector using method 1 is: %d\n", determinant);
}
//Transpose an array.
void transposeArray() {
cout << "IN TRANS";
arr1 = getArray(0);
printArray(arr1, 3, 3);
//Flip the values
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[coll][row];
}
}
cout << "----------" << endl << "The new vector looks like: \n";
printArray(ans, q, w);
}
/*
Multiply arrays. One option is to just multiply it by a number and the other is to multiply it by another array.
*/
void multiplyArrays() {
arr1 = getArray(0);
int h1 = q;
int w1 = w;
cout << "Do you wish to multiply the first vector by a number(Enter 1), or by a second vector(Enter 2)?";
cin >> qq;
int mu;
//First Option is to multiply it by a single number
if (qq == 1) {
cout << "What number do you wish to multiply the vector by?";
cin >> mu;
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[row][coll] * mu;
}
}
printArray(ans, h1, w1);
//Multiply two arrays
} else if (qq == 2) {
arr2 = getArray(0);
int h2 = q;
int w2 = w;
int n1 = h1;
int n2 = w2;
int nCommon = n1;
if (n2 == nCommon) {
cout << "Amount of columns for vector 1 must match amount of rows for vector 2";
return;
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon; k++) {
ans[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
printArray(ans, n1, n2);
}
}
You never allocate memory for ans. Just like you need to allocate storage for the two input arrays before filling them, you need to allocate storage for the answer.
A segmentation fault is generated when you attempt to write to memory that you do not have access to. In this case, because the ans array was not initialized, it points to random memory. When you do ans[row][coll] = arr2[row][coll] - arr1[row][coll];, you get a segfault because ans[row][col] is pointing somewhere outside your program space.
The problem is that you have not allocated memory for the ans array, but you are writing to it in the following code:
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr2[row][coll] - arr1[row][coll];
}
}
This is why you have segmentation fault.
Try to add a block to allocate memory for ans at first.
Having trouble with the binary_search function listed at the top. not sure where to go with it. I'm not very familiar with binary searching.
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void get_input(ifstream& fin, int a[], int size, int & array_size);
void binary_search (int a[], int & array_size)
{
cout << "Please enter the element you would like to search for \n";
int element;
cin >> element;
int lastindex=array_size-1, startindex=0;
while (startindex <= lastindex)
{
int midindex=(array_size/2);
if(element > a[midindex])
{
startindex=midindex;
}
else if (element < a[midindex])
{
lastindex=midindex-1;
}
}
}
int main()
{
int array_size=-1;
int a[100];
ifstream fin;
get_input (fin, a, 100, array_size);
binary_search (a, array_size);
return 0;
}
void get_input (ifstream& fin, int a[], int size, int & array_size)
{
fin.open("numbers.txt");
if (fin.fail())
{
cout << "File failed to open";
exit(1);
}
for(int i = 0; i < size; i++)
{
a[i] = 0;
}
cout << "The numbers in the array are: \n\n";
for (int i = 0; i < size; i++)
{
if (!fin.eof())
{
fin >> a[i];
array_size ++;
}
}
for (int i = 0; i < array_size; i++)
{
cout << a[i] << " ";
}
cout << "\n\n\n";
cout << "The numbers in the array sorted are: \n\n";
for(int i = 0; i < array_size; ++i )
{
int temp2 = a[i];
for (int j = i+1; j < array_size; ++j )
{
if( a[j] < temp2)
{
temp2 = a[j];
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < array_size; i++)
{
cout << a[i] << " ";
}
cout << "\n\n\n";
fin.close();
}
when done the program is suppose to take an input from a file assign it to an array then sort the array. After this i need to use a binary search to find a number given by the user and display its place in the array to the user.
update: getting wrong output for the index found.... should i just add one to midindex?
void binary_search (int a[], int & array_size)
{
cout << "Please enter the element you would like to search for \n";
int element;
cin >> element;
int lastindex=array_size-1, startindex=0;
while (startindex <= lastindex)
{
int midindex= startindex + (lastindex - startindex) / 2;
if(element > a[midindex])
{
startindex=midindex+1;
}
else if (element < a[midindex])
{
lastindex=midindex-1;
}
else if (element == a[midindex])
{
cout<<"Element "<<element<<" found at index "<<midindex<<endl;
return;
}
}
}
Try changing
startindex=midindex;
to:
startindex=midindex + 1;
and
int midindex=(array_size/2);
to
int midindex= startindex + (lastindex - startindex) / 2
and most importantly you are doing nothing when you find the element !!
if(element == a[midindex]) {
cout<<"Element "<<element<<" found at index "<<midindex<<endl;
return;
}
My first reaction is to change the line
int midindex=(array_size/2);
to
int midindex = startindex + (lastindex - startindex) / 2;
Also, don't you want to report if the sought element was found or not? To detect the case when the element is found, another if branch like the following
if( element == a[midindex] )
can be inserted. That can have a return element; or return midindex inside it coupled with a return failure; outside the loop.
EDIT: I made a casual attempt to write a version of binary search. I don't claim it to be correct, as binary search is (in)famous for getting incorrect. Some code with test cases and output is uploaded at codepad.
Snippet:
int *
mybsearch( int const * const a, size_t const n, int const key ) {
int * lo = const_cast< int * >( a );
int * hi = lo + n;
while( lo <= hi ) {
int * const mid = lo + (hi - lo) / 2;
int const midelem = *mid;
if( key == midelem ) {
return mid;
}
else if( key < midelem ) {
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return NULL;
}
The main and test code:
int main() {
int const arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
size_t const num = sizeof( arr ) / sizeof( int );
int * pos20 = mybsearch( arr, num, 20 );
assert( pos20 && (*pos20 == 20) );
int * pos25 = mybsearch( arr, num, 25 );
assert( !pos25 );
int * pos5 = mybsearch( arr, num, 5 );
assert( !pos5 );
int * pos105 = mybsearch( arr, num, 105 );
assert( !pos105 );
}
Binary search works nicely as a recursive algorithm. Pass in the array and length, check the middle value, and recurse on the upper / lower half of the array, as appropriate.
Consider carefully what is not right about int midindex=(array_size/2); when array_size = 1. Then generalize to array_size = 3. Then to any odd number. This will require small run simulations in your head or on paper.
You're close. You want to do something like this:
int binary_search ...
so you can return the index of the element
while (startindex < lastindex)
{
int midindex=(startindex+endindex)/2;
if(element = a[midindex]) {
return midindex;
}
else if(element > a[midindex])
{
startindex=midindex+1;