I was practicing my c++ skills and I went into a question.
I have an array of 20 elements, 10 of them were declared before EX: list[1,2,3,4,5,6,7,8,9,10].
My job was to write a function that inserts the last elements
but after each element of the existing ones for EX: list[1, 0, 2, 8, 3, 9, 4, 10, 5 ...] etc.
what I did is declaring the last 10 elements to 0
void insertNum(int list[], int &count){
srand(time(NULL));
count = 20;
int temp = 0;
int i, j, min;
for (int i = 10; i < count; i++) {
list[i] = 0;
}
}
but I couldn't find the complete solution and it's killing me.
any ideas about how to do it?
this the whole code
#include <iostream>
#include <ctime>
using namespace std;
const int CAP = 20;
void buildList(int[], int &count);
void printList(int[], int count);
void insertNum(int list[], int &count);
int main(){
int list[CAP], count = 0;
buildList(list, count);
cout << "Original List!" << endl;
printList(list, count);
insertNum(list, count);
cout << "List after inserts!" << endl;
printList(list, count);
return 0;
}
void buildList(int list[], int &count){
srand(time(NULL));
count = 10;
for (int i = 0; i < count; i++)
{
list[i] = rand() % 100;
}
}
void printList(int list[], int count){
for (int i = 0; i < count; i++)
{
cout << list[i] << endl;
}
}
void insertNum(int list[], int &count){
}
I didn't understand the question fully, But if I am right you want to create a function that inserts one element after each element of an existing array. This might be what you are looking for. If not please rephrase the problem.
srand(time(NULL)); // need to do in main function only.
int oldArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int finalArray[20] = { 0 };
for (int i = 0; i < 20; i++)
{
int newElement = rand()%11+10; //random number between 10 to 20. Figure out what and from where new element will come from
if (i % 2 == 0)
finalArray[i] = oldArray[i / 2];
else
finalArray[i] = newElement;
}
Related
I apologize if this has been asked, but I ran into a coding question, which was supposed to be simple but I struggled on. Please provide a link if already answered (I may just be bad at searching).
Question: Given the sample code fill in the function to return only unique values in the array. Values must keep order.
Example Input : 1, 2, 3, 10, 4, 3, 2, 10, 1, 11, 6
Example Output: 1 2 3 10 4 11 6
Below is my solution, but I can not seem to think of an easy solution that does not include the use of a vector to store unique values. The tester did not like the use of a vector so I can only assume additional headers / libraries were unacceptable. Any other solutions? I am guessing the tester was looking for the array to be filtered in place.
#include <iostream>
#include <vector> //I was not allowed to add this...
//Function to fill in...
int fxn(int *a, int size)
{
std::vector<int> temp;
for(int i(0); i < size; ++i)
{
bool found(false);
for(auto j : temp)
{
if( j == a[i])
{
found = true;
break;
}
}
if(!found)
{
temp.push_back(a[i]);
}
}
int *ptr_a = &a[0];
for(auto j : temp)
{
*ptr_a = j;
++ptr_a;
}
return size - temp.size();
}
//The rest untochable...
void print(int *a, int size)
{
for(int i(0); i < size; ++i)
{
std::cout << a[i] << " ";
}
std::cout << std::endl;
}
int main(void)
{
int a[] = { 1, 2, 3, 10, 4, 3, 2, 10, 1, 11, 6 };
int size = 11;
int count = fxn(a, size);
print(a, size - count);
return 0;
}
Admittedly, this problem would be easier if you could use external libraries, but if you are certain you cannot, it is still solvable.
I read the question incorrectly the first time. Here is a link to as similar question.
#include<iostream>
using namespace std;
int removeDuplicates(int arr[], int n)
{
int j = 0;
for (int i=0; i < n; i++){
for(int j=0;j<i;j++){
if(arr[i]==arr[j]){
n--;
for (int k=i; k<n; k++){
arr[k]=arr[k+1];
}
i--; // you forgot to decrement i
}
}
}
return n;
}
i am trying to create a list that stores 10 objects. and then use the list to call functions from my class.
here is the code showing what i have tried.
class numbers {
private:
int indexCount;
public:
int randomize(int arr[], int n) {
indexCount = 0;
for (int i = n - 1; i > 0; i--) {
int j = rand() % (i + 1);
indexCount++;
swap(&arr[i], &arr[j]);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << "random calls: " << indexCount << endl;
}
};
int main() {
srand(time(NULL));
list<numbers> list1;
int arr[] = {1, 2, 3, 4, 5, 6, 0, 0, 0};
int n = sizeof(arr) / sizeof(arr[0]);
numbers num[10];
for (int i = 0; i < 10; i++) {
list1.push_back(num[i]);
}
for (int i = 0; i < 10; i++) {
list1[i].randomize(arr, n);
list1[i].printArray(arr, n);
}
return 0;
}
in particular if i change this line
list <numbers> list1;
to
vector <numbers> list1;
the code works fine.
currently i am getting this error
no match for operator[](types are list and int
still new to learning lists as well.
EDIT: this is as a uni task, i am aware that using rand() is not efficient and there are other ways however my code is following the task requirement.
And std::list does not support random access (operator[]).
You need to iterate over the list using iterators, by e.g. using a range-base loop:
for (auto &item : list1) {
item.randomize(arr, n);
item.printArray(arr, n);
}
The task: Create a function that takes a list of numbers as a parameter, and returns a list of numbers where every number in the list occurs only once
As far as I know, functions can't return arrays. But if a function's parameter is an array, it will be automatically a reference parameter, so it will "overwrite" the input array even if it's a void function. Is there any way to overwrite (as reference parameter) the input array with a smaller one?
To be specific: in the code below I would like to overwrite the number[10] array with the newArray[6]
I just started to learn code this week, this is a practice task for me, so I would like to use C++ basics to solve this one, without pointers and more complex stuff. If it's not possible, it's okay too.
#include <iostream>
#include <string>
void selectionSort(int[], int);
void unique(int[], int);
void print(int[], int);
int main(int argc, char *args[]) {
int numbers[] = {1, 11, 34, 11, 52, 61, 0, 1, 34, 1, 61, 72};
int size = sizeof(numbers) / sizeof(int);
unique(numbers, size);
return 0;
}
void unique(int arr[], int size) {
selectionSort(arr, size);
int newSize = 1;
for (int i = 0; i < size - 1; ++i) {
if (arr[i] < arr[i + 1]) {
newSize++;
}
}
int newArray[newSize];
int index = 0;
for (int i = 0; i < size - 1; ++i) {
if (arr[i] < arr[i + 1]) {
newArray[index] = arr[i];
++index;
}
}
newArray[newSize - 1] = arr[size - 1];
print(newArray, newSize);
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size; i++) {
int min = i;
for (int j = i; j < size; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
std::swap(arr[i], arr[min]);
}
}
void print(int arr[], int size) {
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
This is not valid C++:
int newArray[newSize];
That's VLA, which is C99, only available with gcc.
Instead, do:
int* newArray = new int[newSize];
Return this:
return std::make_pair(newArray, newSize);
As you need to return the size as well!! Even if you can overwrite the input array (you can, obviously, depends on your contract, the documentation of your function), you need to return the new size.
But you may want to take a real C++ class.
I am trying to delete any duplicates but not having much success..
void deleatingRepeatingElement (int myArrayLength, int myArray[])
{
for (int i = 1 ; i < myArrayLength; i++){
// start at second index because you don't need to compare the first element to anything, it can't have duplicate that comes first
for (int j = 0; j < i ; j++){
if (myArray[i] == myArray[j]){
myArray[j] = myArray[j + 1];
myArrayLength--;
}
}
}
}
I think there were two main mistakes:
You didn't shift all of the following items when deleting.
You didn't "reset" after deleting.
Here is annotated code that seems to work:
#include <iostream>
/* Remove element at given index from array
* Returns the new array length
* (Note that "int array[]" means exactly the same as "int *array",
* so some people would consider "int *array" better style)
*/
int arrayRemoveAt(int index, int array[], int arrayLength)
{
// Check whether index is in range
if (index < 0 || index >= arrayLength)
return arrayLength;
for (int i = index + 1; i < arrayLength; i++)
{
array[i - 1] = array[i];
}
return arrayLength - 1;
}
/*
* Returns the new length of the array
*/
int deleatingRepeatingElement(int myArrayLength, int myArray[])
{
for (int i = 1; i < myArrayLength; i++)
{
// start at second index because you don't need to compare the first element to anything, it can't have duplicate that comes first
for (int j = 0; j < i; j++)
{
if (myArray[i] == myArray[j])
{
myArrayLength = arrayRemoveAt(i, myArray, myArrayLength);
// After deleting an entry, we must "reset", because now the index i
// might point to another number, which may be a duplicate
// of a number even before the current j.
// The i-- is so that after i++, we will end up with the same i
i--;
break;
}
}
}
// Important: The caller needs this for looping over the array
return myArrayLength;
}
int main(int argc, char **argv)
{
int array[] = {5, 6, 2, 1, 2, 6, 6};
int newSize = deleatingRepeatingElement(7, array);
for (int i = 0; i < newSize; i++)
{
std::cout << array[i] << std::endl;
}
return 0;
}
If you use a static array (such as in my example, as opposed to a dynamic one), you may consider using std::array or a template construction as shown in https://stackoverflow.com/a/31346972/5420386.
Here is the solution to your problem:
#include <iostream>
#include <set>
#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0])))
using namespace std;
int *deleteRepeatedElements(int myArray[], int arrayLength) {
set<int> setArray (myArray, myArray+arrayLength);
int setLength = setArray.size();
static int myPointer[4];
int i = 0;
for (set<int>::iterator it = setArray.begin(); it != setArray.end(); ++it) {
myPointer[i] = *it;
i++;
}
return myPointer;
}
int main() {
int myArray[6] = {5, 3, 5, 6, 2, 4};
int arrayLength = ARRAY_SIZE(myArray);
int* myPointer = deleteRepeatedElements(myArray, arrayLength);
int pointerLength = sizeof(myPointer)/sizeof(*myPointer);
for (int* i = &myPointer[0]; *myPointer != 0; i = ++myPointer) {
cout << *i << " ";
}
cout << '\n';
return 0;
}
I'm trying to write a function that calculates the sum of an array, but when i declare int size = 0; , the function runs 0 times because i=0 ; i
int arraChec(int arra[]) {
int size = 0;
int sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(arra1) << endl;
system("pause");
}
Pass in the array size as a parameter:
#include <iostream>
int arraChec(int arra[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1, 7) << std::endl;
}
Or use std::vector:
#include <iostream>
#include <vector>
int arraChec(std::vector<int>& arra) {
int sum = 0;
for (int i = 0; i < arra.size(); i++) {
sum += arra[i];
}
return sum;
}
int main() {
std::vector<int> arra1 = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1) << std::endl;
}
If you are referring to some C style (sizeof(arra) / sizeof(*arra)) construct I suggest you refrain from using it.
You need to pass two arguments to the function--either the beginning of the array plus the size, or the beginning and (one past the) end, as is conventional in C++:
int arraChec(int* begin, int* end) {
int sum = 0;
for (int* it = begin; it < end; ++it) {
sum += *it;
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(std::begin(arra1), std::end(arra1)) << endl;
system("pause");
}
Of course, you can implement is using the standard library:
cout << std::accumulate(std::begin(arra1), std::end(arra1), 0) << endl;
Use std::array instead of fixed size C-style array.
#include <iostream>
#include <array>
#include <numeric>
using namespace std;
int main() {
array<int, 7> arr = { 2, 3, 5, 7, 8, 9, 1 };
cout << accumulate(arr.begin(), arr.end(), 0) << endl;
return 0;
}
Output
35
Read more about std::accumulate.
Another way not mentioned yet is:
template<size_t N>
int arraChec(int (&arra)[N]) {
int sum = 0;
for (size_t i = 0; i < N; i++) {
sum = sum + arra[i];
}
return sum;
}