I'm trying to write a function which sort an array and I met a problem. When I sort an array it always return 0 in first position of sorted array. No matter what combination it is.
void sorting(int wielkosc)
{
int t[wielkosc];
//filling
for (int i = 0; i < wielkosc; i++)
{
t[i] = rand()% 100 + 1;
}
// before sort
cout << "before sorting: " << endl;
for (int i = 0; i < wielkosc; i++)
{
cout << "[" << i << "] -> " << t[i] << endl;
}
cout << endl;
// asc...
int iTemp = 0;
for (int i = 0; i < wielkosc; i++)
{
for (int j = i + 1; j <= wielkosc; j++)
{
//jak chcesz zmienic na malejaco to se przekrec '<' na '>'
if (t[j] < t[i])
{
iTemp = t[i];
t[i] = t[j];
t[j] = iTemp;
}}}
// after sorting
cout << "after sorting: " << endl;
for (int i = 0; i < wielkosc; i++)
{
cout << "[" << i << "] -> " << t[i] << endl;
}
cout << endl;
}
void adde(int& v, char array[5])
{
if (v > 5) {
v = -1;
}
for (int k = 0; k < 5; k++) {
if (array[k] == 'C') {
array[k] = '-';
}
}
v++;
array[v] = 'C';
}
this is my function
int mov = -1;
char item[5];
for (int i = 0; i < 5; i++) {
item[i] = '-';
}
cout << "Initially " << endl;
for (int i = 0; i < 5; i++) {
cout << "[" << i + 1 << " ] ";
}
cout << endl;
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
cout << "After Item 1, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
cout << "After Item 2, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
cout << "After Item 3, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
cout << "After Item 4, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
cout << "After Item 5, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
cout << item[j] << " ";
}
cout << endl;
My code works fine till here.
After the last item i want my cursor to point back to first index
but shows weird Library Run time error
After it runs this highlighted part of code
cout << "After Item 6, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) { cout << item[j] << " "; } }
Heading ##You did a simple mistake. No need for a debbugger here. Arrays in C/C++ start with index 0. So if you have an array of size 5, like in your example char item[5];, the valid indices are 0,1,2,3,4. But not 5.
In your function adde you simply have the wrong boundary check. In your very first statement you have written:
if (v > 5) {
then later
v++;
array[v] = 'C';
So, if v is >3, for example 4 or 5, you will increment it and access array[5] or array[6], which is out of bounds and produces an error.
You may simply correct it and use
if (v > 3) {
as you first statement. This will fix the problem:
#include <iostream>
void adde(int& v, char array[5])
{
if (v > 3) {
v = -1;
}
for (int k = 0; k < 5; k++) {
if (array[k] == 'C') {
array[k] = '-';
}
}
v++;
array[v] = 'C';
}
int main() {
int mov = -1;
char item[5];
for (int i = 0; i < 5; i++) {
item[i] = '-';
}
std::cout << "Initially " << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << "[" << i + 1 << " ] ";
}
std::cout << std::endl;
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 1, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 2, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 3, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 4, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 5, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
std::cout << "After Item 6, " << std::endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
std::cout << item[j] << " ";
}
std::cout << std::endl;
return 0;
}
Please additionally note:
The design is not very good. Please do not use C-style arrays in C++. Please avoid pointer decays in calls to functions. Please rethink you complete design. I can see, what you are doing. This can be done easier in C++, but for a really good or approriate solution, we need to know the requriements.
I'm stuck on this program: I have to limit the input but the program just totally ignore the if statement if i enter a value greater or equal to 100 it should
display an error but continue to ask user for input and run
Here is my code:
#include<iostream>
using namespace std;
int main()
{
int a[4][4], big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
if (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
else
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}
I've added a while to repeat the check until the number falls below 100. Please note the line "int a[4][4],.." will overrun if you exceed 4 rows * 4 columns. I suggest moving its initialization to a lower position when rows and columns are known.
Try this :
#include<iostream>
using namespace std;
int main()
{
int big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
while (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
int a[m][n];
//else redundant
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}
So for a lab project the goal is to print out shapes using a "*."
The four basic shapes are a square, two downwards pointing right triangles, and a hollow square. Everything seems to be working fine for any integer I input but I'm not entirely sure I'm doing the loop for the hollow square correctly and would like it if someone could give me some advice.
#include <iostream>
using std::cout; using std::endl; using std::cin;
int main()
{
cout << "Input arbitrary integer ";
int n;
cin >> n;
cout << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 2; j <= i; j++)
{
cout << " ";
}
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
{
if (i == 1)
cout << "*";
else if (j == 1)
cout << "*";
else if (i == n)
cout << "*";
else if (j == n)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
My program has errors and wont let me compile. The only errors start at system("pause) though but I dont see why I'm having errors because I did it the same as I always do my programs. Can anyone see what might be the issue? heres the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
//Declarations
int SIZE = 10;
int NUMBERS[10];
int i;
int j;
int temp;
for (int i = 0; i < SIZE; i++)
{
cout << "Please enter a number: " << endl;
cin >> NUMBERS[i];
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (NUMBERS[j] > NUMBERS[j+1])
{
temp = NUMBERS[j];
NUMBERS[j] = NUMBERS[j+1];
NUMBERS[j+1] = temp;
}
}
}
cout << "Sorted List" << endl;
cout << "===========" << endl;
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
system("pause");
return 0;
}
for (int i = 0; i < SIZE; i++) {
// ^^^ add this missing bracket
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
^^^ // closing bracket, but has no opening match
Of course you can skip braces in this case ( as there is only one single line in for body) so this is also solution:
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;