I'm getting "Expected Error" on else if function - for-loop

your text`Working on a for loop program, and I'm getting this errors.
mvb8835_hw4_q2.cpp:35:9: error: expected expression else if(piece < 5){ ^
Below is my code! Any advice/suggestions would be most appreciated!
Thank you!
if(integer >= 100){
piece = (integer / 100);
if(piece >= 5){
roman += 'D';
}
for(i = 0; i < piece - 5; i++){
roman += 'C';
}
else if(piece < 5){
for(i = 0; i < piece; i++){
roman += 'C';
}
}
integer %= 100;
}

Looks like you're not closing the if clause properly. Did you mean to do something like this?
if(piece >= 5){
roman += 'D';
for(i = 0; i < piece - 5; i++){
roman += 'C';
}
}
else if(piece < 5){
for(i = 0; i < piece; i++){
roman += 'C';
}
}

Related

How to show "if operator in for operator" in block algorithms?

For Example:
for (i = 0; i < 100; i++){
if (i % 3 == 0){
cout << i;
}
}
How to show this code in code-block algortihms?

How to properly refactor case-based nested loop?

I have 4 nested loops that used based on the case given. The code is quite similar but there is a little difference in the comparison. Please check this code:
If Case 1:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i][j+1])
// do something
}
}
If Case 2:
for (i = 0; i < length; i++) {
for (j = 4; j > 0; j--) {
if (array[i][j] == array[i][j-1])
// do something
}
}
If Case 3:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i+1][j])
// do something
}
}
If Case 4:
for (i = 4; i > 0; i--) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i-1][j])
// do something
}
}
As you can see that the decrement and increment are different too. It is quite hard for me to figure out how to refactor this. They have similar code behavior (duplication) but they have different conditions in the for and if statement on each case.

Which is faster, if inside a loop, or a just the loop?

I am trying to understand some concepts of performance in code.
If there is, for instance, a 2d boolean array with 5 trues inside it,
if we want to change all the elements in the array to false, which code would be faster?
number1:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
number2:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
You can measure it by using console.time() and console.timeEnd():
Number 1:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.046142578125ms
Number 2:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.031982421875ms
The array I used was arr[1][135] big and had 5 values in the second array that were true.
As you can see the loop with the if clause is faster.

c++ char vector, deleting words with numbers in them plus whitespace after bad word

I think I'm pretty close to solving this but I get a bus error on one of the lines. I have a program that reads from a file that takes in all the words buy separating them into characters. Some words have digits or punctuations in them and the program is suppose to go throw and delete those words plus any whitespace after the bad words. Than output the new sentence. I'm went over it a bunch of times and everything looks sound to me right now so I'm stuck. I kept counts for character(Ccount), spaces(Scount), and bad character(bcount) which are any character in the bad word and whitespace following. Here's the code for the part I'm working on.
if(cFlag == true){
int Ccount = 0;
int Scount = 0;
int Bcount = 0;
for(unsigned int i=0; i<chars.size(); i++){
if(isalpha(chars[i])){
if(Bcount == 0 ){
ctemp.push_back(chars[i]);
Ccount++;
Scount = 0;
}
else if(Bcount != 0 && Scount == 0){
ctemp.push_back(chars[i]);
Bcount++;
}
else if(Bcount != 0 && Scount != 0){
for(int i = 0; i < Bcount; i++)
ctemp.pop_back();
ctemp.push_back(chars[i]);
Bcount = 0;
Scount = 0;
}
}
else if(isspace(chars[i])){
if(Bcount == 0){
ctemp.push_back(chars[i]);
Ccount = 0;
Scount++;
}
else if(Bcount != 0){
//for (int i=0; i<Bcount; i++)
//ctemp.pop_back();
ctemp.push_back(chars[i]);
Bcount++;
Scount++;
}
}
else if(ispunct(chars[i]) || isdigit(chars[i])) {
ctemp.push_back(chars[i]);
Bcount = Ccount;
Bcount++;
Ccount = 0;
}
}
chars.clear();
for (unsigned int i=0; i<ctemp.size(); i++)
chars.push_back(ctemp[i]);
}
All I needed was a Ccount++ in the if else statement that doenst have any incrementers in it. I was getting to a new word than deleting all the previous bad spaces and letters but not counting the first letter of the new word in the new Ccount.

Codechef PERMUT2 solution

Following website has the problem.
http://www.codechef.com/problems/PERMUT2
I have been trying to code solution for PERMUT2. My below solution is failing on some test cases. Kindly help me uncover flaws in the below code.
#include <stdio.h>
int a[100000];
int main()
{
int i, j, n, ret;
while(1)
{
scanf("%d", &n);
if(n == 0)
break;
ret = 0;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++)
if(a[i] != i + 1)
ret++;
if(ret % 2 == 0)
printf("ambiguous\n");
else
printf("not ambiguous\n");
}
return 0;
}
You are not checking the right property. if(a[i] != i + 1) ret++; is not the right checking.
You want to check a[a[i] - 1] == i + 1 for all elements on the array:
bool ambiguous = true;
for(i = 0; i < n; i++) {
if (a[a[i] - 1] != i + 1) {
ambiguous = false;
break;
}
}
if(ambiguous)
printf("ambiguous\n");
else
printf("not ambiguous\n");

Resources