Program not proceeding after taking array input in C - arrays

#include<stdio.h>
int main(){
printf("Enter the size of array: ");
int n,a[n],element;
scanf("%d", &n);
printf("Enter the elements of array in ascending order\n");
for(int i=0; i<n; i++){
scanf("%d ", &a[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &element);
int low = 0;
int high = n-1;
int mid = (low+high)/2;
int flag = 0;
while(low<=high){
if(a[mid]==element){
flag =1;
break;
}
if(a[mid]<element){
low = mid + 1;
}
else{
high = mid -1;
}
}
if(flag){
printf("%d found", element);
}
else{
printf("Not found");
}
return 0;
}
When I run the code, I am only able to enter the elements of array and thus the program does not proceed further.
What's wrong with the code?

int n,a[n],element;
Is equivalent of saying
int n;
int a[n];
int element;
Notice something? You're declaring an array of variable size with an uninitialized variable n. This leads to unexpected behaviour. Declare your array after the value of n has been set!
int n,element;
scanf("%d", &n);
int a[n];

On observing your code,
#include<stdio.h>
int main(){
printf("Enter the size of array: ");
int n,a[n],element;
scanf("%d", &n);
printf("Enter the elements of array in ascending order\n");
for(int i=0; i<n; i++){
scanf("%d", &a[i]); // correction: here you had given space after %d...
}
printf("Enter the element to be searched: ");
scanf("%d", &element);
int low = 0;
int high = n-1;
int mid = (low+high)/2;
int flag = 0;
while(low<=high){
if(a[mid]==element){
flag =1;
break;
}
if(a[mid]<element){
low = mid + 1;
}
else{
high = mid -1;
}
}
if(flag){
printf("%d found", element);
}
else{
printf("Not found");
}
return 0;
}
you had given space scanf("%d ", &a[i]); after %d which should not be used, that's why your program is not proceeding after taking array input... I am assuming your binary search operation is correct and working fine...

Related

Finding pairs in an array that are equal to an input value

I have to find out if there is any pair of i,j such that array[i]^2 + array[j]^2 == x^2
.
If there are such pairs, I need to print all such (i,j). Otherwise, print “There are no such pairs”.
#include <stdio.h>
int main(){
int size=10, i, x,j;
int Array[size];
printf("What is the value of x:");
scanf("%d",&x);
for(i=0;i<size;i++){
printf("Enter array value :");
scanf("%d",&Array[i]);
}
for(i=0;i<size;){
for(j=i+1;j<size;j++)
if((Array[i]*Array[i])+(Array[j]*Array[j])==x*x) //how do I complete this for loop?
}
return 0;
}
Yo're almost there, why weren't you incrementing the value of i? Keep a counter to count the matched pairs, then print those or if nothing is found print whatever you want.
#include <stdio.h>
int main() {
int size = 10, i, x, j;
int Array[size];
printf("What is the value of x:");
scanf("%d", &x);
for (i = 0; i < size; i++) {
printf("Enter array value :");
scanf("%d", &Array[i]);
}
int counter = 0;
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++)
if ((Array[i] * Array[i]) + (Array[j] * Array[j]) == x * x) {
printf("%d %d\n", Array[i], Array[j]);
counter++;
}
}
if (!counter) {
printf("There are no such pairs\n");
}
return 0;
}

How can I get all matching values with a binary search?

Here is my code for finding the position of a value in a sorted array using a binary search. My problem is that the value may be present in my array more than one time, but my search only shows the position of the first match it finds. I want to output my all matching value's position. How can I solve my issue?
Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
void aSort(int *a, int aLen){
int i, j;
for(i=0; i<aLen-1; i++){
for(j=i+1; j<aLen; j++){
if(a[i] > a[j]){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return;
}
void bSearch(int *a, int aSize, int key,int *rArr, int *ItemNum){
int start = 0;
int aEnd = aSize-1;
int mid = (start + aEnd) / 2;
*ItemNum = -1;
while(start<=aEnd){
if(key == a[mid]){
*ItemNum += 1;
rArr[*ItemNum] = mid;
break;
}else if(a[mid] < key){
start = mid + 1;
}else{
aEnd = mid - 1;
}
mid = (start + aEnd) / 2;
}
return;
}
int main(){
int n;
char i_type;
printf("Enter number of array element: \n");
scanf("%d", &n);
int a[n];
printf("Insert array element manual or automatic?\nIf manual press 'M' or press 'A' for automatic\n");
scanf("%*c%c", &i_type);
if(i_type == 'a' || i_type == 'A'){
printf("The array elements is:\n");
int i;
for(i=0; i<n; i++){
a[i] = rand()%100;
printf("%d ", a[i]);
}
}else{
printf("Enter %d integer value: ", n);
int i;
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
printf("The array elements is:\n");
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
}
aSort(a, n);
printf("\nNow the array is in ascending order:\n");
int i;
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
printf("\nEnter the key value which you want to find: ");
int key, numArr, rArr[n];
scanf("%d", &key);
bSearch(a, n, key, rArr, &numArr);
if(numArr == -1){
printf("Item not found!\n");
}else{
int i;
for(i=0; i<=numArr; i++)
printf("Your item found in %d position.\n", rArr[i]+1);
}
return 0;
}
If there are likely to be many repeated values: Do two binary searches, to find the positions of the start and the end of the run of "key" values. Don't terminate the search on exact match, and don't skip over mid; use < for the condition in one search and <= for the other. [The details will be tricky and may need some debugging for cases where the key isn't found or is at one end.]
If there are likely to be few repeated values: Do a single search to find the first occurrence. Then, scan linearly to find the "other end".

Remove the last element inside an array

My problem: I do not know how to delete the last element entered inside the array. The program should stop when the user enters a negative number and should not include the negative number inside the array. I tried, but I cant find the solution. This is my Code:
int main () {
float array[20];
float max ,min;
float rem;
int i;
char op;
for (i = 0; i <= 20; i++){
printf("Enter the element inside array[%d]: ",i);
scanf("%f", &array[i]);
if (array[i] < 0)
break;
}
printf("Enter ......: ");
scanf("%s", &op);
switch (op){
case 'h':
max = array[0];
for (i = 0; i < 20; i++){
if (max < array[i]){
max = array[i];
}
}
printf("The biggest number is: %f\n", max);
break;
case 'l':
min = array[0];
for (i = 0; i < 20; i++){
if (min > array[i]){
min = array[i];
}
}
printf("The smallest number is: %f\n", min);
break;
}
return 0;
}
Please help , I run out of ideas.
Thank you very much.
Simply check entered value before storing in array:
int main () {
float array[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float max ,min;
float rem;
int i;
float a;
char op;
for (i = 0; i < 20; i++){
printf("Enter the element inside array[%d]: ",i);
scanf("%f", &a);
if (a < 0)
break;
array[i]=a;
}
i am sorry for my last answer but this is true ;)
#include<stdio.h>
int length(const float *array) {
int count = 0;
while(array[count]) count++;
return count;
}
void main () {
float array[20];
float max ,min;
float rem,input;
int i;
char op;
int length = 0;
for (i = 0; i < 20; i++){
printf("Enter the element inside array[%d]: ",i);
scanf("%f", &input);
if (input < 0)
break;
length++;
array[i] = input;
}
printf("Enter ......: ");
scanf(" %c", &op);
switch (op){
case 'h':
max = array[0];
for (i = 0; i < 20; i++){
if (max < array[i]){
max = array[i];
}
}
printf("The biggest number is: %f\n", max);
break;
case 'l':
min = array[0];
for (i = 0; i < length; ++i){
if (min > array[i]){
min = array[i];
}
}
printf("The smallest number is: %f\n", min);
break;
}
system("pause");
}
Use a temporary variable before , if it is positive store in array , if not take necessary actions.
for (i = 0; i < 20; i++){
printf("Enter the element inside array[%d]: ",i);
scanf("%f", &x) // declare x as float x;
if (x< 0) // if negative break
break;
array[i]=x; // store into array
}
And also some problems in your program -
printf("Enter ......: ");
scanf("%s", &op); // op is char variable use %c specifier
^ use instead %c
Write like this instead-
scanf(" %c", &op);
this first loop access index out of bound causing UB
for (i = 0;i<=20; i++){ // change condition to i<20 (index can go from 0 to 19)
//your code // as array is declared as float array[20]
}
Both the loops for max and min can go like this -
for (i= 0;array[i]; i++){ // no need to keep track of length of array
Click on link to see working code.

Number of occurrence of an element in a sorted array

I want to find the number of occurrence of an element in a sorted array. I used BinarySearch logic to implement this. But I am not getting the correct output. I am using this logic
numberOfOccurrence = findLastOccurrence - firstOccurrence + 1
This is my code
#include<stdio.h>
int find_last(int a[],int n,int key)
{
int low = 0;
int high = n-1;
int result = -1;
while(low<=high)
{
int mid = (low+high)/2;
if(a[mid]==key)
{
result = mid;
low = mid+1;
}
else if(key<a[mid])
high = mid-1;
else
low = mid+1;
}
return result;
}
int find_first(int a[],int n,int key)
{
int low = 0;
int high = n-1;
int result = -1;
while(low<=high)
{
int mid = (low+high)/2;
if(a[mid]==key)
{
result = mid;
high = mid-1;
}
else if(key<a[mid])
high = mid-1;
else
low = mid+1;
}
return result;
}
int find_count(int a[],int n,int x)
{
int first,last;
first = find_first(a,n,x);
last = find_last(a,n,x);
printf("\nLast index is %d",last+1);
printf("\nFirst index is %d",first+1);
printf("\nlast-first+1 is %d",last - first + 1);
return (last-first+1);
}
main()
{
int a[10],flag=0,n,x,count=0,i;
printf("\nEnter the number of elements ");
scanf("%d",&n);
printf("\nEnter %d elements ",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n Elements are \n");
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n Enter the key ");
scanf("%d",&x);
count = find_count(a,n,x);
printf("%d","\n The count is %d \n",count);
}
but some problem in the statement return (last-first+1);. It is returning some large values.
I tested in CFree with mingw and Visual Studio 2010.
printf("%d","\n The count is %d \n",count);
change it to :
printf(" The count is %d \n",count);
You connot put more than one " " in printf
You don't even need a binary search for this (and in any case the array would need to be sorted). A simple traverse of the array is all that is needed, whether or not it is sorted. Plus a little error checking for good form (GIGO), which makes this program look much bulkier than it would without. The key part of the code is only 3 lines.
#include<stdio.h>
#define NUMBERS 10
int main ( ) {
int a[NUMBERS], n, x, count=0, i;
printf("\nEnter the number of elements ");
if (1 != scanf("%d",&n))
return 1; // bad entry
if (1 > n || NUMBERS < n)
return 1; // number out of range
printf("\nEnter %d elements ", n);
for(i=0; i<n; i++){
if (1 != scanf("%d", &a[i]))
return 1; // illegal number
}
printf("\nElements are \n");
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
printf("\nEnter the key ");
if (1 != scanf("%d", &x))
return 1; // bad entry
for(i=0; i<n; i++){ // <--- every element
if (x == a[i]) // <--- find matching key
count++; // <--- keep a tally
}
printf("\nThe count is %d\n", count); // (corrected) result
return 0;
}

C Program Help: Unexpected Output

School project for Computer Science. I need to make a program where the user declares a size for an array, then fills the array in numerical, nondecreasing order, then declares a value, x. X is then assigned to the appropriate spot so the entire array is in numerical, nondecreasing order. The array is then output.
The code builds properly with no errors, but the output is messed up.
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
size++;
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
while(i=0 <= x && x > ary[i]){
i++;
j = size - 1;
while(j >= i) {
ary[j++] = ary[j];
j--;
}
}
for(i = 0; i < size; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main
Example Output:
Enter the size of the array: 5
Enter digits to fill the array, in numerical order: 1
2
3
4
5
Input x, the value to add to the array: 6
1,2,3,4,5,1630076519,
Process returned 0 (0x0) execution time : 8.585 s
Press any key to continue.
It's always the last value that messes up to that huge number. I cannot for the life of me figure out why. The project is due by midnight EST.
For the while loop, can you try this instead,
i = 0;
while (i < x && x > ary[i]) {
i++;
j = size - 1;
while (j >= i) {
j++;
ary[j] = ary[j]; // Equivalent to ary[j++] = ary[j];, yet easier to read
j--;
}
}
Try this:
#include <stdio.h>
int main (void) {
//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;
int temp1,temp2;
//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);
for(i=0;i<size;i++)
{
if(ary[i]>x)
{
temp1 = ary[i];
ary[i] = x;
break;
}
}
if(i==size)
{
ary[i]= x;
}
else
{
for(j=i+1;j<size+1;j++)
{
if(j==size) //Last element of the new array
{
ary[j] = temp1;
break;
}
temp2 = ary[j];
ary[j] =temp1;
temp1 = temp2;
}
}
for(i = 0; i < size+1; i++) {
printf("%d,", ary[i]);
}
return 0;
} //main

Resources