I am trying to add numbers one after the other to my array and so far I have this:
int main(){
DynamicArray<int> intArray(5);
int num = 0;
int point;
intArray.addElement(20);
intArray.addElement(10);
intArray.addElement(30);
intArray.addElement(60);
intArray.addElement(21);
for(int x = 0; x < 5; ++x){
intArray[x] = rand() % 100;
std::cout << intArray[x] << " ";
}
std::cout << endl;
std::cout << "\n" "Enter the number :" << endl;
cin >> num;
std::cout << "\n" "What point ?" << endl;
cin >> point;
for(int x = 0; x > point; --x){
intArray[x] = intArray[x-1];
}
intArray[point] = num;
for(int x = 0; x < 5; x++){
std::cout << intArray[x] << " ";
}
std::cout << endl;
return 0;
}
but when I run it I get this:
7 49 73 58 30
Enter the number :
2
What point ?
0
2 49 73 58 30
the question is how do I add the element 2 to position 0 without getting rid of the 7 and by resizing the array?
Thank you any assistance would be appreciated.
To resize the DynamicArray you need to set the length properties to the value you want, in your case lenght + 1 to do this you can use this:
intArray.Length = intArray.Length + 1;
Then you can move and add your element as you want.
Pay attention to the index you use in the for cicle, it can contains some errors.
Note: For the code above, maybe is better to use the std::vector.
Related
I am trying to finish my homework, but I have encountered some problems in the item of Duplicate Elements. I have tried to find out where the problem is, but I cannot find it. My code works fine when the sequence is small, but it becomes problematic when the sequence is large.
In this sequence, I expect to get the number in the picture, but I always get the number in the second picture. As you can see, there is always a problem in this item in the duplicate element.
#include <iostream>
using namespace std;
int main()
{
int count = 0;
int arrayA[25];
int arrayB[25];
int min,max;
int num;
int i = 0;
int n = 0;
int temp = 0;
int sum = 0;
//Input
cout << "Input the number of elements to store in the array: ";
cin >> n;
cout << n << endl;
cout << "Input "<< n <<" integers:" << endl;
//Store arrayA
if(count < n){
for(i = 0; i < n; i++){
cin >> num;
cout << "integer - " << count;
cout << " : " << num <<endl;
arrayA[count] = num;
count++;
}
}
//store arrayB
for(i = 0;i < count; i++){
arrayB[i] = arrayA[count - i - 1];
}
//Forwards Array
cout << "The values stored into the array are :" << endl;
for(i = 0;i < count; i++){
cout << arrayA[i] << " ";
}
cout << endl;
//Backwards Array
cout << "The values stored into the array in reverse are :" << endl;
for(i = 0; i < count; i++){
cout << arrayB[i] <<" ";
}
cout << endl;
//Sum
for(i = 0; i < count; i++){
sum += arrayA[i];
}
//Max & Min
for(i = 0; i < count;i++){
if(max < arrayA[i]){
max = arrayA[i];
}
}
for(i = 0; i < count; i++){
if(min > arrayA[i]){
min = arrayA[i];
}
if(arrayA[i]== 0){
min = 0;
break;
}
}
//Duplicate elements
count = 0;
for(i = 0; i < n; i++){
for(temp = i + 1; temp < n; temp++){
if(arrayA[i] == arrayA[temp] ){
count++;
break;
}
}
}
cout << "The sum of all elements of the array is ";
cout << " " << sum << endl;
cout << "The total number of duplicate elements in the array is ";
cout << count << endl;
cout << "The maximum and minimum element in the array are ";
cout << min << " , " << max;
return 0;
}
You should try debugging more by printing, to know what is happening. You are not really taking into account that a number can be repeated several times and depending on the spread, it will count them several times. And here is why:
//Duplicate elements
count = 0;
for(i = 0; i < n; i++){
for(temp = i + 1; temp < n; temp++){
//cout << "Comparing "<< arrayA[i] << " to "<< arrayA[temp]<<endl;
if(arrayA[i] == arrayA[temp] ){
cout << "ITEM DUPLICATED at i , "<<i<< " item "<< arrayA[i]<< " and temp "<< temp<< " item "<< arrayA[temp] << endl;
count++;
break;
}
}
}
ITEM DUPLICATED at i , 0 item 4 and temp 7 item 4
ITEM DUPLICATED at i , 2 item -2 and temp 10 item -2
ITEM DUPLICATED at i , 4 item -3 and temp 14 item -3
ITEM DUPLICATED at i , 5 item 3 and temp 11 item 3
ITEM DUPLICATED at i , 7 item 4 and temp 8 item 4
ITEM DUPLICATED at i , 12 item -1 and temp 13 item -1
As you can see, there is a 4 - 4 twice. It can be seen quite easily if you do it in paper (although you shouldn't)
4A -4B -2C 1D -3E 3F 5G 4H 4I 0J -2K 3L -1M -1N -3O
4a -4b -2c 1d -3e 3f 5g 4h 4i 0j -2k 3l -1m -1n -3o
Connections made:
4A 4h
-2C -2k
-3E -3o
3F 3l
4H 4i
So the code technically is doing what you are asking it to do. if you want to count UNIQUELY (if a 4 appears 15 times, count it as only that 4 is repeated), you should be checking if the number has already been checked. Using a vector or map (dictionaries in C++) can allow you to do that! Or lists with lists (the element, and the number of occurences)
I am making a program that removes a number from the index that the user provides from the array, displays the new array, then asks the user to insert a number at whatever index they choose. The first part of this program works fine when it comes to removing an index, but I am having trouble with adding an index and number. For example, if the NEW array after the user deletes the number from index 5 is: 12 34 45 2 8 16 180 182 22, which is correct if you remember that arrays start at 0, then they request for example index 5 to be added again with the number 78, it gets messed up. It displays 12 34 45 2 78 8 16 180 182 22 (and then it also outputs the number -858993460 for some reason?) SO the problem basically is that it adds the new index and number one index BEFORE it is supposed to. Im sorry if this sounds so confusing but I have been stuck on it for hours. Thank you!
//This program demos basic arrays
#include <iostream>
using namespace std;
const int CAP = 10;
int main()
{
int size;
int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
size = 10;
int i, delIndex, addIndex, newInt = 0;
cout << "Your list is: " << endl;
for (i = 0; i < CAP; i++)
{
cout << list[i] << endl;
}
//Deleting an index
cout << "\nPlease enter index to delete from: ";
cin >> delIndex;
for (i = delIndex; i <= 10; i++)
{
list[i] = list[i + 1];
}
cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
for (i = 0; i < (size - 1); i++)
{
cout << list[i] << endl;
}
//Adding an index
cout << "\nNow, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "\nEnter the number to add to the index: " << endl;
cin >> newInt;
for (i = size - 1; i >= addIndex - 1; i--)
{
list[i + 1] = list[i];
}
list[addIndex - 1] = newInt;
size++;
cout << "The number has been added at the specified index position." <<
endl;
cout << "The new array is: " << endl;
for (i = 0; i < size; i++)
{
cout << list[i] << endl;
}
return 0;
}
The problem is with handling size variable in your code.
Following is corrected code. See it working here:
#include <iostream>
using namespace std;
const int CAP = 10;
int main()
{
int size;
int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
size = CAP;
int i, delIndex, addIndex, newInt = 0;
cout << "Your list is: " << endl;
for (i = 0; i < size; i++)
{
cout << list[i] << endl;
}
//Deleting an index
cout << "\nPlease enter index to delete from: ";
cin >> delIndex;
for (i = delIndex; i < size; i++)
{
list[i] = list[i + 1];
}
size--;
cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
for (i = 0; i < size; i++)
{
cout << list[i] << endl;
}
//Adding an index
cout << "\nNow, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "\nEnter the number to add to the index: " << endl;
cin >> newInt;
for (i = size - 1; i >= addIndex; i--)
{
list[i + 1] = list[i];
}
list[addIndex] = newInt;
size++;
cout << "The number has been added at the specified index position." <<endl;
cout << "The new array is: " << endl;
for (i = 0; i < size; i++)
{
cout << list[i] << endl;
}
return 0;
}
Note: Since we are talking about index and not position in the array, so it is treated as 0 based and element is added at index addIndex, not at addIndex-1.
There are some loopholes in your program, that you can improve.
You are declaring a constant (CAP), but the array size is changing, so point doing that.
You are printing the array multiple times, its better to use a function, and call it every time, when you need to print the entire array.
You are using literals like 10, in some sections of the program. It is advisable to use the same variable like size or CAP instead of 10 to improve readability.
You can even put insert & delete in functions so that they can be called multiple times.
This is a sample working code, you can take the idea of that. I haven't implemented Step 4, I hope you can do that easily,
LIVE CODE
Working Code
//This program demos basic arrays
#include <iostream>
using namespace std;
int CAP = 10;
void printArr(int list[]){
for (int i = 0; i < CAP; i++)
cout << list[i] << " ";
cout<<endl;
}
int main()
{
int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
int i, delIndex, addIndex, newInt = 0;
cout << "Your list is: " << endl;
printArr(list);
//Deleting an index
cout << "\nPlease enter index(0 indexed based) to delete from: ";
cin >> delIndex;
for (i = delIndex; i < CAP - 1; i++)
list[i] = list[i + 1];
CAP--;
cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
printArr(list);
//Adding an index
cout << "\nNow, please enter the index position(0 indexed based) to add to: " << endl;
cin >> addIndex;
cout << "\nEnter the number to add to the index: " << endl;
cin >> newInt;
for (i = CAP; i > addIndex; i--)
list[i] = list[i-1];
list[addIndex] = newInt;
CAP++;
cout << "The number has been added at the specified index position." << endl;
cout << "The new array is: " << endl;
printArr(list);
return 0;
}
So I need some help with this. I want to print out all integers between 2 and 2^20 that are integer powers of 2. I figured out that I need to increase the power by 1 each time but I can't seem to figure out what goes inside the inner for loop. I cannot use the pow() function
c = 2;
cout << "\nPROBLEM C" << endl;
for (int powerC = 1; powerC <= 20; powerC++) // powerC is exponent
{
cout << setw(5) << powerC << " ";
counterC++;
for (int x = 1; x <= 20; x++) // where I am having trouble with
{
c = (c*powerC);
cout << setw(5) << c;
} // end inner for loop
if (counterC % 8 == 0)
{
cout << endl;
}
}
cout << "\nNumber of numbers = " << counterC;
This is much simpler by using the << operator.
Since 2 is 2^1, you want to print all integers from 2^1 to 2^20 inclusively, or 20 numbers:
int c = 2;
for (int i=0; i<20; i++)
{
std::cout << c << std::endl;
c <<= 1;
}
I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}
Ok, I also need to calculate and display each height after a 5% increase using a separate 10-element array. Any ideas? Sorry about all this. This is my first time using arrays.
#include <iostream>
using namespace std;
int main()
{
int MINheight = 0;
double height[10];
for (int x = 0; x < 10; x = x + 1)
{
height[x] = 0.0;
}
cout << "You are asked to enter heights of 10 students. "<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter height of a student: ";
cin >> height[x];
}
system("pause");
return 0;
}
Simply loop like this:
MINheight = height[0];
for (int x = 1; x < 10; x++)
{
if (height[x] < MINheight)
{
MINheight = height[x];
}
}
std::cout << "minimum height " << MINheight <<std::endl;
Side Note: you should not name a local variable starting with Capital letter, using x as array index is also kind of strange, though they both work fine, but not good style.
You may also use std::min_element as follows:
std::cout << *std::min_element(height,height+10) << std::endl;
//^^using default comparison
To put elements in separate array with increased heights and display them, do the following:
float increasedHeights[10] = {0.0};
for (int i = 0; i < 10; ++i)
{
increasedHeights[i] = height[i] * 1.05;
}
//output increased heights
for (int i = 0; i < 10; ++i)
{
std::cout << increasedHeights[i] << std::endl;
}
Essentially, you can keep track of the minimum value as it is being entered, so:
cout << "You are asked to enter heights of 10 students. "<< endl;
MINheight = numerical_limits<int>::max
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter height of a student: ";
cin >> height[x];
if(height[x] < MINheight)MINheight = height[x];
}
cout << "Minimum value was: " << MINheight << "\n";
What this does is create a variable with its value the maximum possible value, then when ever a new value is entered by the user, check if it less than current minimum, if so store it. Then print out the current minimum at the end.