Basic C program: Given an integer n and n int numbers. Create a program that fin
ID: 3722050 • Letter: B
Question
Basic C program:Given an integer n and n int numbers. Create a program that finds what is the missing number, the missing number will be replaced by zero. Your input(the array values) are sorted. You are only missing one value, the numbers are not duplicated. Always start as 1,2,3... however, the first number can be missing. Basic C program:
Given an integer n and n int numbers. Create a program that finds what is the missing number, the missing number will be replaced by zero. Your input(the array values) are sorted. You are only missing one value, the numbers are not duplicated. Always start as 1,2,3... however, the first number can be missing.
Given an integer n and n int numbers. Create a program that finds what is the missing number, the missing number will be replaced by zero. Your input(the array values) are sorted. You are only missing one value, the numbers are not duplicated. Always start as 1,2,3... however, the first number can be missing. nk Creat e a progyam at Senods uwhat .n2 Always stats ,2,3hewe n2
Explanation / Answer
Since the array is sorted and the missing number is replaced by 0, the given problem can be looked as finding the index of the array with value 0. Since the array is sorted and has distinct values, the value at index i should be i+1(since the values start from 1).
Another method of solving this Problem is to find the sum of all the n elements in the array and subtracting it from the sum of first n integers gives the missing number.
PFB code for the given functionality and output of test run:
-------------------------------------------------------
#include <stdio.h>
int findIndexOfMissigNumber(int *arr, int n);
void main(){
int arr[10] = {1,2,3,4,5,0,7,8,9,10};
int indexOfMissingNumber = findIndexOfMissigNumber(arr, 10);
printf("Missing Number is %d ", indexOfMissingNumber+1);
}
int findIndexOfMissigNumber(int *arr, int n){ // same as finding the index in array with value 0
for(int i=0; i<n; i++){
if(arr[i] == 0){
return i;
}
}
}
Test Run:
-----------------------------------
$ gcc prog.c
$ ./a.out
Missing Number is 6
$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.