List<List<int>> arr = [];
List<int> bubbleSort(List<int> list) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length - 1; j++) {
if (list[j] > list[j + 1]) {
int num = list[j];
list[j] = list[j + 1];
list[j + 1] = num;
arr.add(list);
}
}
}
return list;
}
void main(){
bubbleSort([5, 4, 3, 2, 1]);
print(arr);
}
I implemented Bubble sort in dart and after every swap i'm adding the list to the arr but i'm getting an unexpected output:
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
Somehow all the contents of arr are the same.
Please explain how to resolve this and why this is happening.
void swap(List<int> arr, int xp, int yp) {
var temp = arr[xp];
arr[xp] = arr[yp];
arr[yp] = temp;
}
List<int> applyBubbleSort(List<int> arr) {
var n = arr.length;
for (var i = 0; i < n; i++) {
for (var j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) swap(arr, j, j+1);
}
}
return arr;
}
void main(List<String> args) {
var result = applyBubbleSort(
// [5, 3, 1, 9, 8, 2, 4, 7]
[5, 4, 3, 2, 1]
);
print(result);
}
Output:
[1, 2, 3, 4, 5]
Related
void Data::Paramters()
{
for (int i = 0; i < I; i++)
{
mc[i] = new int[K];
for (int k = 0; k < K; k++)
{
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
}
}
}
getting "Too Many Initializer Values" error in starting of { 4, 2, 2, 1, 3 }
where I=5 and K=4
The initializer is for a 4x5 2D matrix, so you should just write:
enum { I = 4, K = 5 };
int mc[I][K] = { { 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 } };
The expression mc[i][k] has the type int. It is not an array.
So this assignment statement
mc[i][k] = {{1, 0, 2, 3, 5},{ 4, 2, 2, 1, 3 }, { 4, 3, 4, 1, 3 }, { 3, 5, 6, 4, 2 } };
does not make a sense.
If K is a constant expression then you can allocate and initialize the two-dimensional array the following way
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
Here is a demonstration program.
#include <iostream>
int main()
{
const size_t K = 5;
size_t I = 4;
int ( *mc )[K] = new int[I][K]
{
{ 1, 0, 2, 3, 5 },
{ 4, 2, 2, 1, 3 },
{ 4, 3, 4, 1, 3 },
{ 3, 5, 6, 4, 2 }
};
for ( size_t i = 0; i < I; i++ )
{
for (const auto &item : mc[i])
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
The program output is
1 0 2 3 5
4 2 2 1 3
4 3 4 1 3
3 5 6 4 2
I would like to add the peak position of a vector and the peak number but I can't find a way to add the elements and then return it, or write it on console.
#include <iostream>
#include <vector>
using namespace std;
struct PeakData {
vector<int> pos, peaks;
};
PeakData pick_peaks(vector<int> v) {
PeakData result;
for (int i = 1; i < v.size() - 1; i++) {
if ((v[i] > v[i - 1]) && (v[i] > v[i + 1])) {
result.peaks.push_back(v[i]);
result.pos.push_back(i);
}
}
return result;
}
Example: pickPeaks([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]) should return {pos: [3, 7], peaks: [6, 3]}
Add this to your main function:
int main()
{
vector<int> a = {3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3};
PeakData stPickPeaks = pick_peaks(a);
vector<int> :: iterator itr;
for(itr = stPickPeaks.pos.begin(); itr<stPickPeaks.pos.end(); itr++)
{
cout <<*itr<<endl;
}
for(itr = stPickPeaks.peaks.begin(); itr<stPickPeaks.peaks.end(); itr++)
{
cout <<*itr<<endl;
}
return 0;
}
Also try to pass the parameters either by reference or pointer.
This is my code which task is to check how many times does the number appear in my array.
Error message tells me that vector subscript is out of range.
int main() {
vector<int> numbers(1,-1);
int x;
int z;
bool exit;
int digits[] = { 2, 4, 5, 3, 2, 5, 6, 3, 5, 7, 9, 2, 1, 2, 3, 4, 5, 6, 4, 3, 2, 6, 3, 4, 4, 1, 3, 7, 9, 5, 9, 2, 3, 1, 2, 3, 4, 5, 6, 2, 1, 2, 3, 4, 5, 3, 2, 7, 7, 7 };
for (int i = 0; i < (sizeof(digits) / sizeof(int)); i++) {
z = 0;
bool exit = 1;
for (int j = 0; j < (sizeof(digits) / sizeof(int)); j++) {
x = digits[i];
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
if (x == numbers[y]) {
bool exit = 0;
}
}
if (digits[j] == x && exit) {
z++;
}
}
if (exit) {
cout << "Liczba: " << x << " wystepuje: " << z << " razy" << endl;
numbers.push_back(x);
}
}
}
Visual studio tells me that my code is valid but then during compilation , cmd shows me the error message which tells me that Vector subscript out of range . I just can't figure it out how to fix it exactly , bcs i have not found that i would initialize the vector which would not exist .
At least this statement
for (int y = 0; y < (sizeof(numbers) / sizeof(int)); y++) {
does not make sense because the expression sizeof(numbers) / sizeof(int) does not yield the number of elements in the vector. Instead use
for (int y = 0; y < numbers.size(); y++) {
If I have correctly understood your task then instead of the container std::vector it is better to use either std::map or std::unordered_map.
For example
#include <iostream>
#include <map>
int main()
{
int digits[] =
{
2, 4, 5, 3, 2, 5, 6, 3, 5, 7,
9, 2, 1, 2, 3, 4, 5, 6, 4, 3,
2, 6, 3, 4, 4, 1, 3, 7, 9, 5,
9, 2, 3, 1, 2, 3, 4, 5, 6, 2,
1, 2, 3, 4, 5, 3, 2, 7, 7, 7
};
std::map<int, size_t> frequency;
for ( const auto &item : digits )
{
++frequency[item];
}
for ( const auto &p : frequency )
{
std::cout << p.first << ": " << p.second << '\n';
}
return 0;
}
The program output is
1: 4
2: 10
3: 10
4: 7
5: 7
6: 4
7: 5
9: 3
Is this linear complexity implementation of circular array rotation correct?
n = number of elements
k = number of rotations
int write_to = 0;
int copy_current = 0;
int copy_final = a[0];
int rotation = k;
int position = 0;
for (int i = 0; i < n; i++) {
write_to = (position + rotation) % n;
copy_current = a[write_to];
a[write_to] = copy_final;
position = write_to;
copy_final = copy_current;
}
No.
Consider this example.
#include <iostream>
int main(void) {
int n = 6;
int k = 2;
int a[] = {1, 2, 3, 4, 5, 6};
int write_to = 0;
int copy_current = 0;
int copy_final = a[0];
int rotation = k;
int position = 0;
for (int i = 0; i < n; i++) {
write_to = (position + rotation) % n;
copy_current = a[write_to];
a[write_to] = copy_final;
position = write_to;
copy_final = copy_current;
}
for (int i = 0; i < n; i++) {
std::cout << a[i] << (i + 1 < n ? ' ' : '\n');
}
return 0;
}
Expected result:
5 6 1 2 3 4
Actual result:
3 2 1 4 1 6
Using stl::rotate on std::array, you can left rotate by, say 2, as:
std::array<int, 6> a{1, 2, 3, 4, 5, 6};
std::rotate(begin(a), begin(a) + 2, end(a)); // left rotate by 2
to yield: 3 4 5 6 1 2, or right-rotate by, say 2, as:
std::rotate(begin(a), end(a) - 2, end(a)); // right rotate by 2
to yield: 5 6 1 2 3 4, with linear complexity.
Rotate an Array of length n for k times in left or right directions.
The code is in Java
I define a Direction Enum:
public enum Direction {
L, R
};
Rotation with times and direction:
public static final void rotate(int[] arr, int times, Direction direction) {
if (arr == null || times < 0) {
throw new IllegalArgumentException("The array must be non-null and the order must be non-negative");
}
int offset = arr.length - times % arr.length;
if (offset > 0) {
int[] copy = arr.clone();
for (int i = 0; i < arr.length; ++i) {
int j = (i + offset) % arr.length;
if (Direction.R.equals(direction)) {
arr[i] = copy[j];
} else {
arr[j] = copy[i];
}
}
}
}
Complexity: O(n).
Example:
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Rotate 3 times left
Output: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
Input: [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]
Rotate 3 times right
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The questions pretty much sums it up. Here's what I have so far:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
for(int i = 0; i < 8; i++) {
}
}
I'm planning on assigning the sum of each value from sum1 to sum8 to a row, then order each row by its total sum and display it to the user.
However, I keep getting stuck and can't find any good documentation. Can anyone help me write an effective function I can loop through in my for loop to add each of the rows to a sum, then return the sum and average all the rows out? (this will likely have to be more than one function but...whatever.)
If you want the sum of each row displayed in ascending order, the following code will do that...
// #include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int sums[8] = {0};
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
// calculate the sums
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
sums[i] += a[i][j];
}
}
sort(begin(sums), end(sums)); // sort the sums
copy(begin(sums), end(sums), ostream_iterator<int>(cout, " ")); // display the sums
return 0;
}
I see you are a cool programmer. You do not seek an easy way.
Of course you could use an array of sums but it seems it is too easy.
Well, then try the following
#include <iostream>
#include <numeric>
int main()
{
int sum1;
int sum2;
int sum3;
int sum4;
int sum5;
int sum6;
int sum7;
int sum8;
int a[8][7] = {
{ 2, 4, 3, 4, 5, 8, 8 } ,
{ 7, 3, 4, 3, 3, 4, 4 } ,
{ 3, 3, 4, 3, 3, 2, 2 } ,
{ 9, 3, 4, 7, 3, 4, 1 } ,
{ 3, 5, 4, 3, 6, 3, 8 } ,
{ 3, 4, 4, 6, 3, 4, 4 } ,
{ 3, 7, 4, 8, 3, 8, 4 } ,
{ 6, 3, 5, 9, 2, 7, 9 }
};
size_t i = 0;
for ( auto p : { &sum1, &sum2, &sum3, &sum4, &sum5, &sum6, &sum7, &sum8 } )
{
*p = std::accumulate( a[i], a[i] + 7, 0 );
++i;
}
for ( int x : { sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8 } ) std::cout << x << std::endl;
}
The output is
34
28
20
31
32
28
37
41
Having the sums you can find any average. I hope you will do this yourself.