Have to compare equality of 5 numbers and choose best of 3 in case in C - equality

I have a situation where i have to write a code to compare 5 integers to see which ones are equal and choose atleast 3 equal numbers of the 5 nrs. More than:-
e.g.
a=0,b=0,c=0,d=2,c=3
then Valid_nr=0,
a=6,b=0,c=6,d=6,c=6
then Valid_nr=6,
a=8,b=8,c=8,d=8,c=8
then Valid_nr=8
Please suggest me some logic because this seems to be very confusing to write a short and efficient code to achieve this..

Example written in C#:
class Program
{
static List<int> numbers = new List<int>();
static int NumOfequals = 1;
static int ValidNUmber;
static void Main(string[] args)
{
for (int i=0;i<5;i++)
{
int input;
Console.WriteLine("Enter number " + (i+1).ToString() + "and press enter");
if (int.TryParse(Console.ReadLine(),out input))
{
numbers.Add(input);
}
else
{
Console.WriteLine("not an integer");
}
}
numbers.Sort();
for (int i=0;i<numbers.Count;i++)
{
if (i<numbers.Count-1)
{
if (numbers[i]==numbers[i+1])
{
NumOfequals++;
ValidNumber=numbers[i];
}
else
{
if (NumOfequals < 3)
{
NumOfequals = 1;
ValidNumber=-1;
}
}
}
}
Console.WriteLine("number of equal numbers is " + NumOfequals.ToString()+" and the valid number is:"+ValidNumber.ToString());
Console.ReadLine();
}
}

I'll show you a function in C that generalize the problem in your question, equalities() compares the elements of an array of int and return the maximum number of equalities. A pointer to an int is passed too to store the value of the repeted number. You can easily specialize this function for your problem or use it as it is and check if the returned value is >= 3 only.
#include <stdio.h>
#include <stdlib.h>
int equalities( int *nums, int n, int *x ) {
int * vals = malloc(n * sizeof(int));
int * reps = calloc(n,sizeof(int));
int i,j;
for ( i = 0; i < n; ++i ) {
for ( j = 0; j < n && reps[j] != 0 && vals[j] != nums[i]; ++j );
if ( j != 0 && reps[j] == reps[0]) { // new max
vals[j] = vals[0];
vals[0] = nums[i];
++reps[0];
} else {
vals[j] = nums[i];
++reps[j];
}
}
*x = vals[0];
int rep = reps[0];
free(vals);
free(reps);
return rep;
}
int main(void) {
int test1[] = { 1, 4, 2, 4, 4 };
int test2[] = { 0, 0, 2, 0, 0 };
int test3[] = { 1, 3, 2, 4, 5 };
int test4[] = { 1, 1, 1, 1, 1 };
int test5[] = { 5, 5, 0, 5, 0 };
int y = -1;
int r = equalities(test1,5,&y);
printf("Repetitions: %d, Number: %d\n",r,y);
r = equalities(test2,5,&y);
printf("Repetitions: %d, Number: %d\n",r,y);
r = equalities(test3,5,&y);
printf("Repetitions: %d, Number: %d\n",r,y);
r = equalities(test4,5,&y);
printf("Repetitions: %d, Number: %d\n",r,y);
r = equalities(test5,5,&y);
printf("Repetitions: %d, Number: %d\n",r,y);
return 0;
}
The output of the test main() is:
Repetitions: 3, Number: 4
Repetitions: 4, Number: 0
Repetitions: 1, Number: 1
Repetitions: 5, Number: 1
Repetitions: 3, Number: 5

Related

How can add append two array by method?

public static void main(String[] args) {
// TODO code application logic here
int a[] = { 1, 3, 4 };
int b[] = { 5, 6, 7, 8, 9 };
int[]c = new int[a.length+b.length];
int count = 0;
System.out.println(result(c[a.length+b.length]));
}
public static int result () {
// TODO code application logic here
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + " ");
int count = 0;
return result ;
}
}

Exception thrown at 0x7A12FF80 (ucrtbased.dll) in Project 3.exe: 0xC0000005: Access violation reading location 0x00000000

I have attempted to run this code. Prior to running this, no warnings or errors exist but once it is executed I have an exception thrown and it stops the program from compiling. Here is my code and the error is in the subject line. The CDA file is being used as header file to create a Circular Dynamic Array that is going to be manipulated in Heaps.cpp. The heaps.cpp is to create a binary heap that is to be used in to create Binomial heaps, but that code has not been developed yet.
#include <iostream>
using namespace std;
template <class T>
class CDA
{
private:
int rear;
int size;
int capacity;
T* circArray;
int front;
bool ordered;
T placeHolder;
public:
CDA();
CDA(int s);
~CDA();
int Front();
T Data(int n);
T& operator[](int i);
void AddEnd(T v);
void AddFront(T v);
void DelEnd();
void DelFront();
int Length();
int Capacity();
int Clear();
bool Ordered();
int SetOrdered();
int S01(int n);
T Select(int k);
void InsertionSort();
void QuickSort();
void QuickSort1(int low, int high);
void CountingSort(int m);
int Search(T e);
void reSize();
void Shrink();
int BinarySearch(int left, int right, T e);
int QSortPartition(int low, int high);
void Swap(int* x, int* y);
int QSelPartition(int front, int rear);
T QuickSelect(int front, int rear, int k);
CDA<T>& operator=(const CDA& a);
CDA(const CDA& old);
int Median(int low, int high);
};
template <class T>
CDA<T>::CDA()
{
capacity = 1;
circArray = new T[capacity];
size = 0;
rear = size - 1;
front = -1;
ordered = false;
placeHolder = 0;
}
template <class T>
CDA<T>::CDA(int s)
{
size = s;
capacity = s;
circArray = new T[capacity];
front = 0;
rear = size - 1;
ordered = false;
}
template <class T>
CDA<T>::CDA(const CDA& a)
{
size = a.size;
capacity = a.capacity;
circArray = new T[a.capacity];
front = a.front;
rear = a.size - 1;
ordered = a.ordered;
for (int i = a.front; i < a.front + (a.size); i++)
{
circArray[i % capacity] = a.circArray[i % capacity];
}
}
template <class T>
CDA<T>::~CDA()
{
delete[]circArray;
}
template <class T>
T& CDA<T>::operator[](int i)
{
if (i > capacity)
{
cout << "Array index is out of bounds; exiting." << endl;
placeHolder = i;
cout << endl;
return placeHolder;
exit(0);
}
else
{
return circArray[(front + i) % capacity];
}
}
template <class T>
void CDA<T>::AddEnd(T v)
{
size++;
if (front == -1)
{
circArray[0] = v;
front++;
rear++;
return;
}
if (size > capacity)
{
reSize();
}
else if (front == -1)
{
front = 0;
rear = size - 1;
}
else
{
rear = (rear + 1) % capacity;
}
circArray[rear] = v;
}
template <class T>
void CDA<T>::AddFront(T v)
{
size++;
if (size > capacity)
{
reSize();
}
if (front == -1) //means the array is empty
{
front = 0;
rear = capacity % size;
}
else if (front == 0) //means something is in spot 0
{
front = capacity - 1; //puts front at the end and places the input there
}
else //go until it is back at zero
{
front--;
}
circArray[front] = v;
}
template <class T>
void CDA<T>::DelEnd()
{
size--;
if (size <= capacity / 4)
{
Shrink();
}
else if (rear == front)
{
front = -1;
rear = -1;
}
else
{
rear--;
}
}
template <class T>
void CDA<T>::DelFront()
{
size--;
double shrMeasure;
shrMeasure = capacity / 4.0;
if (size <= shrMeasure) // make an empty and shrink function
{
Shrink();
}
/*
else if (front == rear)
{
if (front == 0)
front = size - 1;
else
front++;
}
*/
else
{
if (front == size) //brings it full circle
{
front = 0;
}
else
{
front++;
}
}
if (front > capacity)
front = front % capacity;
}
template <class T>
int CDA<T>::Length()
{
return size;
}
template <class T>
int CDA<T>::Capacity()
{
return capacity;
}
template <class T>
int CDA<T>::Clear()
{
~CDA();
size = 1;
circArray[size] = NULL;
}
template <class T>
bool CDA<T>::Ordered()
{
return ordered;
}
template <class T>
int CDA<T>::SetOrdered()
{
for (int i = 1; i < size - 1; i++)
{
if (circArray[(i - 1)] > circArray[i])
{
ordered = false;
return -1;
}
}
ordered = true;
return 1;
}
template <class T>
T CDA<T>::Select(int k)
{
if (ordered == true)
{
return circArray[(front + k - 1) % capacity];
}
else
QuickSelect(front, front + (size - 1), k);
}
template <class T>
int CDA<T>::QSelPartition(int left, int right)
{
int pivot = circArray[right % capacity];
int x = left - 1;
//Swap(&circArray[pivIndex], &circArray[right]);
for (int i = left; i <= right - 1; i++)
{
if (circArray[i % capacity] <= pivot)
{
x++;
Swap(&circArray[x % capacity], &circArray[i % capacity]);
}
}
Swap(&circArray[(x + 1) % capacity], &circArray[right % capacity]);
return (x + 1);
}
template <class T>
T CDA<T>::QuickSelect(int left, int right, int k)
{
if (k > 0 && k <= (right - left) + 1)
{
int index = QSelPartition(left, right);
if (index - 1 == k - 1)
return circArray[index % capacity];
else if (index - 1 > k - 1)
return QuickSelect(left, index - 1, k);
else
return QuickSelect(index - 1, right, k - index + left - 1);
}
return -1;
}
template <class T>
void CDA<T>::InsertionSort() //must be utilized in quicksort
{
for (int i = front + 1; i < (front + size); i++)
{
int val = circArray[i % capacity];
int inc = (i - 1) % capacity;
while (inc >= 0 && circArray[inc] > val)
{
circArray[(inc + 1) % capacity] = circArray[inc];
inc--;
if (inc == -1)
{
inc = capacity - 1;
}
}
circArray[(inc + 1) % capacity] = val;
}
ordered = true;
}
template <class T>
void CDA<T>::QuickSort() // change to other quicksort before leaving the ferg
{
QuickSort1(front, front + (size - 1));
}
template <class T>
void CDA<T>::QuickSort1(int low, int high)
{
while (low < high)
{
if (high - low < 900)
{
InsertionSort();
break;
}
else
{
int pivot = QSortPartition(low, high);
if (pivot - low < high - pivot)
{
QuickSort1(low, pivot--);
low = pivot + 1;
}
else
{
QuickSort1(pivot++, high);
high = pivot - 1;
}
}
}
}
template <class T>
int CDA<T>::QSortPartition(int low, int high)
{
int pivot = circArray[Median(low, high) % capacity];
Swap(&circArray[(Median(low, high)) % capacity], &circArray[(high) % capacity]);
int index = low % capacity;
for (int i = low; i < high; i++)
{
if (circArray[i % capacity] <= pivot)
{
T t = circArray[i % capacity];
circArray[i % capacity] = circArray[index % capacity];
circArray[index % capacity] = t;
index++;
}
}
Swap(&circArray[index % capacity], &circArray[high % capacity]);
return index;
}
template <class T>
int CDA<T>::Median(int low, int high)
{
T left, mid, right;
left = circArray[low % capacity];
mid = circArray[((low + high) / 2) % capacity];
right = circArray[high % high];
if (left < right && left > mid)
return low % capacity;
if (left < mid && left > right)
return low % capacity;
if (right < left && right > mid)
return high % capacity;
if (right < mid && right > left)
return high % capacity;
if (mid < left && mid > right)
return ((low + high) / 2 % capacity);
if (mid < right && mid > left)
return ((low + high) / 2 % capacity);
}
template <class T>
void CDA<T>::Swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
template <class T>
void CDA<T>::CountingSort(int m) ////NEED TO FIX THIS
{
int* OP = new int[size];
int* Counter = new int[m + 1];
for (int i = front; i <= rear; i++)
{
cout << "CircArray[" << i << "] is " << circArray[i] << endl;
}
for (int i = 0; i <= m; i++)
{
Counter[i] = 0;
}
for (int i = front; i < front + (size); i++)
{
Counter[circArray[i % capacity]]++;
}
for (int i = 1; i <= m; i++)
{
Counter[i] += Counter[i - 1];
}
for (int i = rear - 1; i > 0; i--)
{
OP[Counter[circArray[i]] - 1] = circArray[i];
cout << "Circular array at " << i << " is " << circArray[i] << endl;
Counter[circArray[i]] -= 1;
if (i == front % capacity)
break;
if (i == 0)
i = capacity;
}
for (int i = 0; i < size; i++)
circArray[i] = OP[i];
ordered = true;
front = 0;
}
template <class T>
int CDA<T>::Search(T e)
{
if (ordered == true) //binary search of item e
{
return BinarySearch(front, front + (size - 1), e);
}
else if (ordered == false)
{
for (int i = 0; i < size - 1; i++)
{
if (circArray[i] == e)
return i;
}
}
return -1;
}
template <class T>
int CDA<T>::BinarySearch(int left, int right, T e)
{
while (left <= right)
{
int mid = (left + right) / 2;
int value = circArray[mid % capacity];
if (value == e)
return (mid - front) % capacity;
else if (value < e)
return BinarySearch(mid + 1, right, e);
else if (value > e)
return BinarySearch(left, mid - 1, e);
}
return -1;
}
template <class T>
void CDA<T>::reSize()
{
capacity = capacity * 2;
T *nArray = new T[capacity];
for (int i = 0; i < size - 1; i++)
{
int l = (front + i) % (size-1);
nArray[i] = circArray[l];
}
//delete[]circArray;
circArray = nArray;
front = 0;
rear = (size - 1);
}
template <class T>
void CDA<T>::Shrink()
{
int tFront = front;
capacity = capacity / 2;
T* bArr = new T[capacity];
int index = 0;
while (front <= rear)
{
bArr[index] = circArray[(front + index) % capacity];
index++;
}
T* circArray = bArr;
front = 0;
rear = (size - 1);
}
template <class T>
CDA<T>& CDA<T>::operator=(const CDA<T>& a)
{
if (this != &a)
{
delete[]circArray;
size = a.size;
capacity = a.capacity;
circArray = new T[a.capacity];
front = a.front;
rear = a.size - 1;
ordered = a.ordered;
for (int i = a.front; i < a.front + (a.size); i++)
{
circArray[i % capacity] = a.circArray[i % capacity];
}
}
return *this;
}
template <class T>
int CDA<T>::Front()
{
return front;
}
template <class T>
T CDA<T>::Data(int n)
{
return circArray[n];
}
#include <iostream>
#include "CDA-1.cpp"
using namespace std;
template<class keytype, class valuetype>
class Heap
{
private:
CDA<keytype>* K;
CDA<valuetype>* V;
int size;
public:
Heap()
{
this->K = new CDA<keytype>();
this->V = new CDA<valuetype>();
size = 0;
}
Heap(keytype k[], valuetype v[], int s)
{
//allocate two different arrays for each type
//fill those arrays concurrently using insert
//sort concurrently using heapafy recursively
this->K = new CDA<keytype>(s);
this->V = new CDA<valuetype>(s);
this->size = s;
for (int i = 0; i < s; i++)
{
insert(k[i], v[i]);
}
heapify(s, K->Front());
}
void heapify(int s, int i)
{
//Errors for evans to fix: swap the n's with s.
//Fix the left and right variable logic. Hepaify smallest not small at the bottom.
//Also we need to pass V[] in a parameter so we can edit it in this.
//How to better swap V with K and not just K.
int smallest = i;
int left = 2*i +(-i+1);
int right = 2*i + (-i+2);
keytype kl = K->Data(left);
keytype kr = K->Data(right);
keytype ks = K->Data(smallest);
if (left < s && kl < ks) //FIX
smallest = left;
if (right < s && kr < ks) //FIX
smallest = right;
if (smallest != i)
{
swap(K[i], K[smallest]);
swap(V[i], V[smallest]); //FIX
heapify(s, smallest);
}
}
~Heap() {
return;
}
//items should be inserted using bottom up heap building method
void insert(keytype k, valuetype v)
{
K->AddEnd(k);
V->AddEnd(v);
heapify(size, K->Front());
}
keytype peekKey()
{
int f = K->Front();
return K->Data(f);
}
valuetype peekValue()
{
int f = V->front();
return V->Data(f);
}
keytype extractMin()
{
keytype temp = K->Data(K->Front());
K->DelFront();
V->DelFront();
heapify(size, K->Front());
return temp;
}
void printKey()
{
ActualPrintKey(K->Front());
}
void ActualPrintKey(int n)
{
keytype rt = K->Data(n);
if (rt != size)
{
cout << K->Data(rt) << " ";
ActualPrintKey((2 * n) + (-n + 1));
ActualPrintKey((2 * n) + (-n + 2));
}
}
};
/*
template <class keytype, class valuetype>
class BHeap
{
BHeap();
BHeap(keytype k[], valuetype v[], int s);
~BHeap();
keytype peekKey();
valuetype peekValue();
keytype extractMin();
//items should be inserted using repeated insertion
void insert(keytype k, valuetype v);
void merge(BHeap<keytype, valuetype>& H2);
void printKey();
};
*/
#include <iostream>
#include "Heaps.cpp"
using namespace std;
int main() {
string K[10] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "K" };
int V[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
Heap<string, int> T1, T2(K, V, 10);
cout << T2.peekKey() << endl;
cout << endl;
system("pause");
return 0;
}
In general "Access violation reading location", means you are trying to read virtually memory address space to a process which does not belong to your application and the operating system protective mechanism is kicking in to protect the rest of the loaded applications and resource from been accessed (read or write) by your application "memory leak vulnerability". If I was at your place I would review the code and all variables and arrays if they are properly initialized before use. Another thing which needs to be taken in consideration is the operating system and the permissions required by your application (Windows run as Administrator / GNU/Linux sudo).
Cheers

For example, if coins = [1, 2, 5] and N = 11, return true if coins = [3, 77] and N = 100, return

wait online。~
Given a number of coins with different denominations, e.g. [1, 2, 5] and test if they could be used to make up a certain amount (N), assuming you can use unlimited number of coins in each denomination. For example, if coins = [1, 2, 5] and N = 11, return true if coins = [3, 77] and N = 100, return
The idea is use a recursive function (here it will calculate with one first coin vs array of other coins then recursively reduce size of coin array ).
But sr I'm not familiar with Objective-C so I write one using C#. Use should convert it to Objective-C.
bool CanDo(int n, int [] arr)
{
if (arr.Length == 1)
{
if (n % arr[0] == 0)
{
return true;
}
}
else
{
var ls = new List<int>(arr);
ls.RemoveAt(0);
int [] newarr = ls.ToArray(); //Create New array by deleting first element(current calculated element) of old array
for(int i = 0; i <= n/arr[0]; i++)
{
int next_n = n - i * arr[0];
if (next_n == 0)
{
return true;
}
else if (next_n < 0)
{
break;
}
else if(next_n > 0)
{
if( CanDo(next_n, newarr) )
{
return true;
}
}
}
}
return false;
}
This is full code in C# that can print to console first found solution.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static List<string> resultString = new List<string>();
static bool CanDo(int n, int [] arr)
{
if (arr.Length == 1)
{
if (n % arr[0] == 0)
{
resultString.Add(n/ arr[0] + "*" + arr[0]);
return true;
}
}
else
{
var ls = new List<int>(arr);
ls.RemoveAt(0);
int [] newarr = ls.ToArray(); //Create New array by deleting first element of old array
for(int i = 0; i <= n/arr[0]; i++)
{
if (resultString.Count > 0)
{
resultString.RemoveAt(resultString.Count - 1);
}
int next_n = n - i * arr[0];
if (next_n == 0)
{
resultString.Add(i + "*" + arr[0]);
return true;
}
else if (next_n < 0)
{
break;
}
else if(next_n > 0)
{
if (i != 0)
{
resultString.Add(i + "*" + arr[0] + " + ");
}
if( CanDo(next_n, newarr) )
{
return true;
}
}
}
}
return false;
}
static void Main(string[] args)
{
try
{
int[] arr = { 3, 5, 7 };
int N = 20;
resultString = new List<string>();
if (CanDo(N, arr))
{
resultString.ForEach(Console.WriteLine);
Console.Read();
}
else
{
Console.Write("Can't do");
Console.Read();
}
}
catch (Exception ex)
{
//handle exception
}
}
}
}

Why is my Index was out of range in MVC 4?

I've tried to create some list with increase number after some calculation each month for a year.
For example, in month 1 number = 1, month 2 number = 3, month 3 number = 5.
The calculation is like this number[i] = i + number[i - 1]. Which i is the month.
I want to show all the list like this
Month[1] = 1,
Month[2] = 3,
Month[3] = 5,
Month[4] = 7,
Month[5] = 9,
Month[6] = 11,
Month[7] = 13,
...
Month[12] = 23
Here's my Controller
for (i = 1; i <= 12; i++)
{
List<int> number = new List<int>();
if (i <= 12)
{
number[i] = a(i, number[i - 1]);
}
else
{
//something else
}
}
Here's my a function
public int a(int month, int number)
{
try
{
a = month + number;
}
catch (Exception ex)
{
throw ex;
}
return a;
}
But, when executed i'm getting this error
Index was out of range. Must be non-negative and less than the size of the collection.
I've change the controller into this
for (i = 0; i <= 12; i++)
{
//...
}
But have the same error. Can someone help me? Why I'm getting this error?
Try this
Fiddle demo
int[] number = new int[13];
for (int i = 1; i <= 12; i++)
{
if (i <= 12)
{
number[i] = a(i, number[i - 1]);
}
else
{
//something else
}
}
int j = 1;
List<int> item = new List<int>();
foreach (var a in number)
{
if (j <= 12)
{
item.Add(a);
}
else
{
break;
}
j++;
}
public int a(int month, int number)
{
int a = 0;
try
{
a = month + number;
}
catch (Exception ex)
{
throw ex;
}
return a;
}

Whats wrong with my code? The backwards loop doesnt work (Option 2)

The option (2) crashes/overloads my coding software, it has the same code as option (1), does anybody know why its doing it and how to fix it?
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
int length();
float mtof(int note);
int main() {
// do while the user hasnt pressed exit key (whatever)
int control[8] = {74, 71, 91, 93, 73, 72, 5, 84};
int index;
int mod;
float frequency;
int notes[8];
int response;
mod = aserveGetControl(1);
// ask backwards, forwards, exit
// SCALING
// (getControl(75) / ((127 - 0) / (1000 - 100))) + 100;
while(true) {
printf("Run Loop Forwards (1), Backwards (2), Exit (0)\n");
scanf("%d", &response);
if(response == 1) {
while(mod == 0) {
for(index = 0; index < 8; index++) {
notes[index] = aserveGetControl(control[index]);
frequency = mtof(notes[index]);
aserveOscillator(0, frequency, 1.0, 0);
aserveSleep(length());
printf("Slider Value:%5d\n", notes[index]);
mod = aserveGetControl(1);
}
}
} else if(response == 2) {
// here is the part where the code is exactly
// the same apart from the for loop which is
// meant to make the loop go backwards
while(mod == 0) {
for(index = 8; index > 0; index--) {
notes[index] = aserveGetControl(control[index]);
frequency = mtof(notes[index]);
aserveOscillator(0, frequency, 1.0, 0);
aserveSleep(length());
printf("Slider Value:%5d\n", notes[index]);
mod = aserveGetControl(1);
}
}
} else if(response == 0) {
return 0;
}
}
}
int length() {
return (aserveGetControl(75)/((127.0 - 0) / (1000 - 100))) + 100;
}
float mtof(int note) {
return 440 * pow(2, (note-69) / 12.0);
}
Your for loops aren't exactly the same.
The first option goes through { 0, 1, ..., 7 }
The second option goes through { 8, 7, ..., 1 }
Notice also that control[8] is undefined (0..7). So when it tries to reference this location the application runs into an error.
Change the second for loop to
for (index = 7; index >= 0; index--) {
...
}