Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 hours ago.
Improve this question
This is my problem. My task is to code the function void:
void process(char input[], char output[]){
}
And the given:
int main(){
const int MAX_SIZE = 100;
char input[] = "this is a beautiful day" ;
char output[MAX_SIZE];
process(input, output);
cout << output;
return 0;
}
Note: I just can use and for this question.
This is my work:
void process(char input[], char output[]){
int n = strlen(input);
for (int i = 0; i > n; i++){
output[i] = input[i];
input[i] = input[n - i + 1];
input[n - i + 1] = output[i];
}
}
But it doesn't work. How should I fix it?
there's a logical error with your code.
for (int i = 0; i > n; i++){
output[i] = input[i];
input[i] = input[n - i + 1];
input[n - i + 1] = output[i];
In your looping statement, you've initialised i=0 and given the condition i>n. This will result in the loop statement not executing.
Secondly, I have simplified your code further, and it should provide the required input.
for (int i = 0; i<=n; i++){
output[i] = input[n - i]; //1
cout<<output[i];
}
So essentially, a string is an array of characters, so to simplify, since the index in the array output is running from 0 to n, simultaneously, the array input indexes are being accessed from n to 0, in the line //1 .
As mentioned, since a string is an array of characters, within the same loop, i'm displaying each element in the output string. Providing the answer.
My test case was : This is a test
Output: tset a si sihT
Hope this helps! Let me know if you need any further explanation.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I'm trying to print the output, but my code keep getting an error:
C++ compile error: ISO C++ forbids comparison between pointer and integer
The error is in else if:
#include <stdio.h>
#include <string.h>
int main (){
char BT [5][100];
for (int i = 1; i<=2; i++){
printf("\nInsert book title:");
scanf("%[^\n]", &BT[i]);
getchar ();
}
printf("\nOur Collections :\n");
for (int i = 1; i<= 2 ; i++){
for (int i = 0; i< strlen(BT[i]); i++){
int k;
if ( i == 0 && BT[i][k] != ' ') {
printf("Shelf code : %c\n", BT[i][k]);
}
else if ( i > 0 && BT[i - 1] == ' ') {
printf("Shelf code : %c\n", BT[i][k]);
}
}
}
return 0;
}
There are many mistakes.
For example indices in C for arrays start from 0 but you are using for loops where indices start from 1 like
for (int i = 1; i<=2; i++){
and below in the code you are trying to access BT[0].
Or the function scanf for reading a string expects an argument of the type char * but you are passing an argument of the type char ( * )[100].
scanf("%[^\n]", &BT[i]);
At least you should write
scanf("%99[^\n]", BT[i]);
Another problem is that you are using the uninitialized variable k to access elements of the array
int k;
if ( i == 0 && BT[i][k] != ' ') {
Moreover due to the fact the the element BT[0] does not have a value the condition in the if statement when i is equal to 0 invokes undefined behavior along with the uninitialized variable k.
Also it is a bad idea to use the same identifier i in the nested loops
for (int i = 1; i<= 2 ; i++){
for (int i = 0; i< strlen(BT[i]); i++){
It seems you mean
for (int i = 1; i<= 2 ; i++){
for (int k = 0; k< strlen(BT[i]); k++){
In this if statement
else if ( i > 0 && BT[i - 1] == ' ') {
the expression BT[i-1] has the type char *. So you are trying to compare a value of the type char with a value of the type char *. You forgot to specify the second index for the array BT.
You compile C program using C++ compiler which is wrong.
BT[i - 1] is an char array acting as lvalue (pointer) not character. You compare it to an integer ' '. Even in C it will raise a warning and it is definitely not what you want.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}
You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I currently have a small assignment for my c++ college course. I'm currently running into an error where it runs the first two loops but then freezes and does not finishes the rest of the loops. the whole point is to printout a * diamond
an example would look like this if you entered the number 7:
*
***
*****
*******
*****
***
*
this is what the code looks like currently:
#include <iostream>
using namespace std;
int main(){
cout<<"How many lines do you want?";
int num_rows;
cin>>num_rows;
int row_average = (num_rows/2)+1;
for(int count=0; count<num_rows; ++count){
int midpoint = row_average - count;
int absolute = abs(midpoint);
int spaces = absolute;
for (int count_a = 0; count_a<spaces; ++count_a){
cout<<" ";
}
for (int count_b = row_average; count_b<num_rows; ++count){
int stars = count_b - spaces;
for(int count_c = 0; count_c = stars; ++count_c){
cout<<"*";
}
}
}
}
Any answers or help would be appreciated!
Thanks!
Is this below a typo?
for (int count_b = row_average; count_b<num_rows; ++count){
---------------------------------------------^^^^^
Shouldn't it be ++count_b?
Also you're not outputting any newline character?
A few things:
1) In the second nested for loop you should increment count_b. So
for(int count_b = row_average; count_b < num_rows; ++count_b) {
2) In the last nested for loop you need to make the condition count_c less than stars, not equal to it. So
for(int count_c = 0; count_c < stars; ++count_c) {
3) Finally, you need a newline character or else all these stars will print on the same line
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
So, I need to get the sum of the first 5 even numbers from my array, this is the code that I've got so far, have no clue what to do next. It runs, but the result is incorrect
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int niz[10];
cout << "Unesi 10 brojeva:";
for (int i = 0; i < 10; i++) {
cin >> niz[i];
}
int suma = 0;
int parni[5];
int j = 0;
for (int i = 0; i < 10; i++) {
if (niz[i] % 2 == 0) {
niz[i] == parni[j];
j++;
if (j == 5) {
break;
}
}
}
for (int i = 0; i < 5; i++) {
suma = parni[i] + suma;
}
cout << suma;
system("PAUSE");
return 0;
}
This line:
niz[i] == parni[j];
does nothing. (It tests if niz[i] happens to be equal to the current, uninitialized value of parni[j], and throws away the result of the comparison.)
You want to store niz[i] in parni[j], so do this:
parni[j] = niz[i];
Incidentally, there is a problem if there are fewer than 5 even numbers in the niz array. In that case, you still sum up all five entries of the parni array, using uninitialized values, which is Bad. One way to avoid this is to just sum up the even entries as you find them, without using a secondary array.
IE, do suma += niz[i] at the line in question, and get rid of parni altogether.
Unless you're really required to use arrays here, vectors will work much more nicely.
You can also use a couple of standard algorithms to make your life easier (especially std::copy_if and std::accumulate).
// for the moment I'll ignore the code to read the input from the user:
auto input = read_input();
auto pos = std::remove_if(input.begin(), input.end(),
[](int i) { return i % 2 != 0; });
// assume that `input` always contains at least 5 elements
pos = std::min(pos, input.begin() + 5);
sum = std::accumulate(input.begin(), pos, 0);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following code. I am trying to populate an array with a deck of cards, and I keep encountering the same error "expected expression error" no matter how I code the loops to populate the array.
Can anybody see where I'm going wrong. I think its something painfully simple, that I, who am new to C++, am just missing.
Thanks!!
#include <iostream>
using namespace std;
struct playingCard{
char suit; // heart (1), club (2), spade (3), diamond (4)
int value; // 1 to 13 (ace is LOW)
};
void printArray(playingCard playingCardArray[], int size){
for (int i = 0; i < size; i ++){
cout << playingCardArray[i].suit << ":\t" << playingCardArray[i].value << endl;
}
}
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 1;
while (suitLoop == 1){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i] = {suitLoop, valueLoop},
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
To resolve your compilation issue change you inner for loop like this:
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i].suit = suitLoop;
playingCardArray[i].value = valueLoop;
}
Other than compilation your code also has Infinite loop , to resolve this you need to change your main somewhat like this:
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 0;
while (suitLoop < ARRAY_SIZE){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[suitLoop].suit = (suitLoop/13 + 1);
playingCardArray[suitLoop++].value = valueLoop;
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
Exchanging the comma with a semicolon at the end of playingCardArray[i] = {suitLoop, valueLoop}, solves the problem.