Increasing array size in C++ [closed] - c++

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 6 years ago.
Improve this question
I have project and this is my code. I am expected to write a console application which finds the largest k numbers in a given file and prints these largest k numbers in descending order. My program should take the filename and k as input parameters from the user.
For example for the following file content:
3, 5, 12, 54, 12, 3, 654, 11, 46, 7, 3
The output for k = 3 should be:
654 54 46
using namespace std;
int main() {
int i, size, p, maxim, k, n, j;
int* a = new int[7000000];
size = 7000000;
ifstream file("7_million_numbers.txt");
if (file.is_open())
{
for (i = 0; i < size; i = i + 1)
file >> a[i];
}
cout << "Enter the number: " << endl;
cin >> n;
for (j = 1; j < n; j = j + 1) {
for (k = 0; k < size - 1; k++) {
maxim = a[k];
p = k;
for (i = k + 1; i < size; i++)
if (a[i] > maxim) {
maxim = a[i];
p = i;
}
a[p] = a[k];
a[k] = maxim;
}
}
for (i = 0; i < n; i++)
cout << a[i] << " " << endl;
system("pause");
delete[] a;
return 0;
}
This code is not working. I had ".exe stopped working" problem. Is it because of an array size? Because text file which is reading by program has a 7 million numbers.

I have a very simple solution to your problem, you can sort the given numbers in descending order before taking the input of the numbers to be shown, after sorting you can ask user "How many greatest numbers to be shown" and when you show the sorted result it will automatically show the greatest n numbers.
Instead of int make it long int.
int main() {
int i, size, p, maxim, k, n, j;
long int* a = new long int[7000000];
size = 7000000;
ifstream file("7_million_numbers.txt");
if (file.is_open())
{
for (i = 0; i < size; i = i + 1)
file >> a[i];
}
cout << "Enter the number: " << endl;
cin >> n;
for(j=0;j<size;j++){
for(k=j;k<size;k++){
if(a[j]<a[k]){
int temp;
temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
for (i = 0; i < n; i++)
cout << a[i] << " " << endl;
system("pause");
delete[] a;
return 0;
}

It seems to me that you're only doing one lookup per run. I would think that you wouldn't need to store all the numbers in memory just the kth largest. to do this a set<int> would work well:
void FindKthLargest( istream& fileIn , ostream& userOut, istream& userIn )
{
int k = 0;
userOut << "Enter the k number: ";
userIn >> k;
set<int> largetNums;
int temp = 0;
for ( int i = 0; i < k; i++ )
{
if ( fileIn >> temp )
{
largetNums.emplace( temp );
}
else
{
userOut << "Number is too big\n";
return;
}
}
while ( fileIn >> temp )
{
set<int>::iterator lowest = largetNums.begin();
if ( temp > *lowest )
{
largetNums.emplace( temp );
largetNums.erase( lowest );
}
}
for ( auto i : largetNums )
{
userOut << i;
}
}

Related

Smallest composite number in array

I am totally new to programming and I am bit stuck on my code. I wrote code where I wanted to find smallest composite number in array(using only low-level arrays). When I wrote down like size of array 3 and enter 1 2 77, than it throws out random 16. Can you explain why is this happening and perhaps give some solution how to fix this.
#include<iostream>
using namespace std;
int fun(int n)
{
int arr[n];
int mini = arr[0];
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
{
for (int j = 2; j < arr[i]; j++)
{
if (arr[i] % j == 0)
{
if (mini > arr[i])
{
mini = arr[i];
}
else
{
mini = mini;
}
break;
}
}
}
return mini;
}
int main()
{
int n;
cout << "Size of array: ";
cin >> n;
cout << "Write " << n << " numbers: " << fun(n) << endl;
return 0;
}

How do I put any value X after all the minimums in an array?

If I enter an array , at first the code finds the minimums then I want to put zeroes after all the minimums . For example
given an array = 1,1,3,1,1
As we see 1s are the minimum so the result should be = 1,0,1,0,3,1,0,1,0
CODE
#include <pch.h>
#include <iostream>
int main()
{
int min = 10000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] > min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the
i++; // `for loop k` to be precise)
n++;
}
std::cout << array[i] << ", 0";
}
return 0;
}
But my answer doen't put zeroes exactly after minimums
There are few issues in your code, first of all your min is wrong. I have fixed your code with comments on fixes I have made. Please take a look :
#include "stdafx.h"
#include <iostream>
int main()
{
int min = 10000;
bool found = 0;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min) //< instead of >
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min)
{
for (int k = n; k > i; k--)
{
array[k] = array[k - 1];
}
array[i + 1] = 0;
i++; //increment i here because you don't want to consider 0 that you have just added above.
n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
}
}
//print the array separately
for (int i = 0; i < n; i++)
{
std::cout << array[i];
if (i != n - 1)
{
std::cout << ",";
}
}
return 0;
}
The first issue was in the calculation of min: < instead of >.
Another problem if that you are modifyng the paramers iand ninside the loop. This is rather dangerous and implies to be very cautious.
Another issue was that it should be i++; n++; instead of i--,n--;
Here is the code:
// #include <pch.h>
#include <iostream>
int main()
{
int min = 1000000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the)
i++;
n++;
}
}
for (int i = 0; i < n; i++) {
std::cout << array[i] << " ";
}
std::cout << "\n";
return 0;
}

Receiving memory-related runtime error & searching for tips on code efficiency

I compiled my code numerous times and receive a memory-related runtime error. I am new to programming and couldn't figure out the issue. If someone can assist me in understanding why this is occurring and give me a few tips on how to clean up my code/make it more efficient I would greatly appreciate it!
#include <iostream>
#include <cmath>
void chefsMenuitems();
void chefsMenuitems(int P[], int arr_size) {
int count = 0;
int current_item = 0;
int n = 0;
int i = 0;
for (int j = 0; j < arr_size; j++ ) {
while(P[j] > 0) {
for ( i = 0; current_item < P[j]; i++) {
current_item = pow(2,(i));
if (current_item > P[j]){
current_item = pow(2,(i - 1));
break;
}
}
P[j] = P[j] - current_item;
current_item = 0;
n++;
}
count++;
std::cout << "The number of menu items for price " << count << " are: " << n << "\n";
n = 0;
current_item = 0;
}
}
int main() {
int T = 0;
int P[] = {0};
int arr_size;
std::cout << "Please enter the number of test cases: \n";
std::cin >> T;
while(T < 1 || T > 5 ) {
std::cout << "Test cases must be between 1 & 5 inclusive: \n";
std::cin >> T;
}
arr_size = T;
for (int i = 0; i < T; i++) {
std::cout << "Please enter the amount you are willing to spend: \n";
std::cin >> P[i];
while(P[i] < 1 || P[i] > pow(10, 5)) {
std::cout << "The amount you are willing to spend must be between 1 and 10^5 inclusive: \n";
std::cin >> P[i];
}
}
chefsMenuitems(P, arr_size);
return 0;
}
Your
int P[] = { 0 };
is an array of one element but you try to access up to 5 elements.
Use a std::vector<int> if you want an array-like container with a size not known at compile-time or waste some space by defining an array of the largest possible size for expected input:
int P[5] = {};

previous row element of array also getting updated

in this program i am separating integers from a character array which consists of a space between them
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
int i = 0, t, l = 0, j, c, k, q = 0, num = 0;
char ch[10][10];
int ach[10][1];
cout << "enter the number of test cases";
cin >> t;
for (i = 0; i < t; i++)
{
fflush(stdin);
cin.getline(ch[i], 9);
}
for (i = 0; i < t; i++)
{
num = 0;
for (j = 0; ch[i][j] != '\0'; j++) //calculating length
{
l = j;
}
l = l + 1;
for (j = 0; j < l; j++)
{
if (ch[i][j] == ' ') //finding the space
c = j;
}
for (k = 0; k < c; k++) //taking first integer out of char array
{
q = ch[i][k] - 48; //parsing char to int
num = (num * 10) + q;
}
cout << "\n previous row element " << ach[0][1] << "\n"; //checking the value
ach[i][0] = num; // this statement is updating the previous row's last element of the array
cout << "\n previous row element " << ach[0][1] << "\n"; //checking the value
cout << ach[i][0];
num = 0;
q = 0;
for (k = c + 1; k < l; k++) //taking second element out of char array
{
q = ch[i][k] - 48; //parsing char to int
num = (num * 10) + q;
}
ach[i][1] = num;
cout << ach[i][1];
}
for (i = 0; i < t; i++)
{
cout << "\n" << ach[i][0] << "\t" << ach[i][1] << "\n"; //displaying the values
}
getch();
return 0;
}
I have marked the code that is malfunctioning , it is updating the previous row's last element. please help.
Oups your code is not really optimized and is mainly C with the exception of cin.getline. But your real problem is that with int ach[10][1], ach is a 2D array of size 10x1, so ach[i][1] may not be what you expect because you should define int ach[10][2] to safely use it. The rules for array indexes computing give &(ach[i][1]) == &ach[0][0] + i*1 + 1 so you are actually accessing ach[i+1][0] with a possible past end array access if i is 9.
Moreover, at first access, ach[0][1] is used without being first initialized.
So your ach definition should be:
int ach[10][2] = {0};

Incorrect Result from Selection Sort Algorithm

#include <iostream>
using namespace std;
// Selection Sort function.
// Parameter 'a' is the size of the array.
void ss(int AR[] , int a) {
int small;
for (int i = 0 ; i <a ; i++) {
small = AR[i];
for (int j = i+1 ; j <a ; j++) {
if (AR[j]< small) {
int k = AR[j];
AR[j] = AR[i];
AR[i] = k;
}
}
}
}
int main() {
cout << "Enter the size of Your Aray";
int a;
cin >> a;
int AR[a];
cout << endl;
for (int i = 0; i < a; i++) {
cin >> AR[i];
cout << endl;
}
ss(AR, a);
cout << "The Sorted Array is";
for (int i=0; i < a; i++) {
cout << AR[i] << " ";
cout << endl;
}
}
When I enter the following:
15
6
13
22
23
52
2
The result returned is:
2
13
6
15
22
23
52
What is the bug preventing the list from being sorted numerically as expected?
The function can look like
void ss ( int a[], size_t n )
{
for ( size_t i = 0 ; i < n ; i++ )
{
size _t small = i;
for ( size_t j = i + 1; j < n ; j++ )
{
if ( a[j] < a[small] ) small = j;
}
if ( i != small )
{
int tmp = a[small];
a[small] = a[i];
a[i] = tmp;
}
}
}
It doesn't seem to be the SelectionSort I know. in the algorithm I know during every loop I look for the smallest element in the right subarray and than exchange it with the "pivot" element of the loop. Here's the algorithm
void selectionSort(int* a, int dim)
{
int posMin , aux;
for(int i = 0; i < dim - 1; ++i)
{
posMin = i;
for(int j = i + 1; j < dim; ++j)
{
if(a[j] < a[posMin])
posMin = j;
}
aux = a[i];
a[i] = a[posMin];
a[posMin] = aux;
}
}
and it seems that you change every smaller element you find, but also change the position of the "pivot". I hope the answer is clear.
Everything is ok in the original function, only that the small variable need to be refreshed when two vector elements will be switched.
Also in if statement set the small variable to the new value of AR[i].