Nested loop with arrays having repeating numbers [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array that keeps the number of each element.
int total[5] = {2,3,4,5,6}
int num = 5; //array total has 5 elements
This means that we have 2 element 0's, 3 element 1's in our original array. We are not worried about the original array since I already have a code to keep the number of the elements.
I need a nested for loop that creates a new array that looks like this:
array[0] = 1;
array[1] = 1;
array[2] = 5;
array[3] = 5;
array[4] = 5;
array[5] = 9;
array[6] = 9;
array[7] = 9;
array[8] = 9;
and so on. That is, we store a value in our new array as many as the its value in "total" array. Values 1,5,9, etc. are stored in an array called element. I have something like this so far:
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[i + j] = element[i];
}
}
Can somebody help me to figure this out?

An easy solution (though not necessarily elegant) is to do the following:
int count = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[count] = element[i];
count++;
}
}
then you don't need to worry about trying to figure out what position you are at for the array.

You need to track the number of the elements:
int sum = 0;
for (int i = 0; i < num; i++){
for (int j = 0; j < total[i]; j++){
array[sum + j] = element[i];
}
sum += total[i];
}

Related

Sort an array by using function in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I want to make a sort statement by using a function. The problem is, I cannot return it back to the main() function. What's the problem? I also tried the void.
int sort_func(int sort[]) {
int swap = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; i++) {
if (sort[j] > sort[j + 1]) {
swap = sort[j];
sort[j] = sort[j + 1];
sort[j + 1] = swap;
}
}
}
return sort[5];
}
What is the problem in the code?
You don't need to return anything. The array is being passed into the function via an int* pointer, so the code is directly manipulating the elements of the caller's array.
Also, your 2nd for loop is incrementing i when it should be incrementing j instead.
How is the caller's array declared, exactly? The loops inside your function require the sort parameter to point at an int[] array with at least 5 elements. However, the function's declaration does not convey or enforce that requirement, so the caller is free to pass in however many elements it wants, and if that size is fewer than 5 then your code will have undefined behavior. You should at least have the caller pass in the actual number of elements as another parameter, eg:
void sort_func(int sort[], int size) {
int swap = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < (size-1); j++) {
if (sort[j] > sort[j + 1]) {
swap = sort[j];
sort[j] = sort[j + 1];
sort[j + 1] = swap;
}
}
}
}
int arr[5];
...
sort_func(arr, 5);
Otherwise, if you strictly require 5 elements, then enforce that by taking a pointer to an array with exactly 5 elements, eg:
void sort_func(int (*sort)[5]) {
int swap = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if ((*sort)[j] > (*sort)[j + 1]) {
swap = (*sort)[j];
(*sort)[j] = (*sort)[j + 1];
(*sort)[j + 1] = swap;
}
}
}
}
int arr[5];
...
sort_func(&arr);
Or, use a reference instead of a pointer, eg:
void sort_func(int (&sort)[5]) {
int swap = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if (sort[j] > sort[j + 1]) {
swap = sort[j];
sort[j] = sort[j + 1];
sort[j + 1] = swap;
}
}
}
}
int arr[5];
...
sort_func(arr);

Regular loop to Pointer loop conversion, how? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
SomeClass* stuff;
int N = 10;
stuff = new SomeClass[N];
Someclass* objectPtrDelete = null;
int i = 0;
for(Someclass* pointer = begin(); pointer != end(); pointer++)
{
if(pointer->getSomeAttr() == randomPointer.getSomeAttr()){
objectPtrDelete = pointer;
break;
}
i++;
}
// Shrinking the C-array with this for loop, shifting left
for (int j = i; j < N-1; j++)
stuff[j] = stuff[j + 1];
Can the last loop be converted into a Pointer loop, if yes, how is this properly done? Note, the names are fictional i.e. something imaginary. I have implemented something similar, but, I would like to understand how can I convert the last loop to a pointer for loop that does the same operation.
Make this:
for (int j = i; j < N-1; j++)
stuff[j] = stuff[j + 1];
Into a Pointer loop.
For starters this construction (without an ending semicolon)
int[] a = new int[N]
is invalid in C++.
It should look like
int *a = new int[N];
If I have understood your question correctly you mean something like the following
for ( auto prev = a, next = a + 1; next != a + N; ++next )
{
*prev++ = *next;
}
(edited to take into account your changes)
if I well understand what you want, having
SomeClass* stuff;
you can replace
for (int j = i; j < N-1; j++)
stuff[j] = stuff[j + 1];
by
for (SomeClass * p = stuff + i; p < stuff + N-1; p++)
*p = *(p + 1);
or to easily manages changes concerning the type :
for (auto p = stuff + i; p < stuff + N-1; p++)
*p = *(p + 1);
etc

How to Increment values of an an array using a nested-loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
So I need help. The problem is that an int arr[5] = {0};
I know the array has the values {0,0,0,0,0} filling the whole array. At the end of the code the array must have the values {1,2,3,4,5} inside it. To solve it must use a nested for loop.
I have tried the following code
Sorry if there is an error in the way this question is formatted this is my first time and using mobile.
int arr[5] = {0};
for(int j = 1; j<6; j++)
{
for(int i = 0; i<5; i++)
{
arr[i] = j;
}
}
I don't know what the benefit of that but If you have to use nested for loop, the following is an option
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 1; i < 2; ++i)
{
arr[j] = j+i;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
Or like #JaMit comment suggests
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 0; i < j+1; ++i)
{
arr[j]++;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
I'm guessing your professor means you should generate the final results using only incrementation. In other words, I'm guessing you're not allowed to assign values after the int arr[5] = {0}; line (so an expression like arr[i] = j; is not allowed).
In that case, you could solve the problem as follows:
int arr[5] = {0};
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
arr[j]++;
}
}
Now we have only used incrementation and the final result is {1, 2, 3, 4, 5}.

Represent spiral of numbers in a matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to dynamically fill the following output in a two-dimensional array with the same number of rows and columns.
Edit: Here is the solution in c++ using #TheGeneral code from c#.
#include <iostream>
using namespace std;
int main(){
int size = 10;
int half = size/2;
int matrix[size][size];
int number1 = 0;
int number2 = 0;
for(int i = 1; i<=size; i++){
for(int j = 1; j<= size; j++){
if(i > half){
number1 = size + 1 - i;
}else{
number1 = i;
}
if(j > half){
number2 = size + 1 - j;
}else{
number2 = j;
}
if(number1 < number2){
matrix[i-1][j-1] = number1;
}else{
matrix[i-1][j-1] = number2;
}
}
}
cout<<"MATRIX:"<<endl;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<"["<<matrix[i][j]<<"] \t";
}
cout<<endl;
}
return 0;
}
For a bit of fun
The only note worthy things going on, are
Starting the index from 1 (makes things easier)
Conditional Operator to say if an index is greater than 5 reverse the count
Math.Min to make it syemtrical
Exmaple
private static int[, ] CreateArray()
{
var ary = new int[10, 10];
for (var i = 1; i <= 10; i++)
for (var j = 1; j <= 10; j++)
ary[i - 1, j - 1] = Math.Min(i > 5 ? 11 - i : i, j > 5 ? 11 - j : j);
return ary;
}
Demo here

Implementation of CountSort [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So to cut a long story short, I am trying to implement countsort for vector. I have some error somewhere in my code, though I think that I have followed the pseudo code pretty strictly.
The function:
void csort(vector<int>& a, vector<int>& b)
{
vector<int> c(a.size());
for(int i = 0; i <= c.size(); i++)
c[i] = 0;
for(int i = 0; i <= a.size(); i++ )
{
c[a[i]]++;
}
for(int i = 0; i <= c.size(); i++)
{
c[i]+= c[i-1];
}
for(int i = a.size(); i < 1; i--)
{
b[c[a[i]]] = a[i];
c[a[i]] =c[a[i]] -1;
}
}
The pesudo code:
let C[k] be a new array
for i = 0 to k
C[i] = 0
for j = 1 to A:length
C[A[j]] = C[A[j]] +1
for i = 1 to k
C[i] = C[i]+ C[i-1]
for j = A:length downto 1
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] -1
The code as it is is very unreadable and error prone. In any case, what I first saw is that in the snippet below you will access c[-1] at the first iteration, thus incurring in undefined behaviour.
for (int i = 0; i <= c.size(); i++)
{
c[i] += c[i-1]; // Evaluation of c[i-1] is illegal for i == 0
}
for(int i = a.size(); i < 1; i--)
This code won't work at all.
for j = A:length downto 1
↑
To implement this pseudo code, you should do like this
for(int i(a.size()-1);i>=0;--i)
And for code like this( c[a[i]] ),you will get run-time error if a[i]<0 or a[i]>=c.size()