expected expression before 'return' and '}' (curly brackets). I have tried changing everything, doesn't run - expected-exception

#include <stdio.h>
int main(){
char a[5];
for(char i = 0, i < 5, ++i){
scanf("%c", &a[i]);
}
printf("%c", a[5]);
return 0;
}
I changed a[5] into a[i] in the printf, nothing changed.
I have compared with this one i found on the net(which works ofc):
int main() {
int values[5];
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

You are using , instead of semi colon ; in the for loop.
for(char i = 0, i < 5, ++i)
Correct Usage:
for(char i = 0; i < 5; ++i)

Related

Why is I getting wronng answer in this code?

Can somebody tell me why I am getting wrong answer in this code? Can somebody help me solve this problem? It is a uva problem. problem number 11060.
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k=0;
while(scanf("%d", &n) !=EOF){
k++;
int c = 0;
string v[n];
map <string, int > mp1;
multimap <int, string> mp2;
string a;
for(int i=0; i<n; i++){
cin >> a;
mp1[a]=0;
}
int m;
cin >> m;
string x, y;
for(int i=0; i<m; i++){
cin >> x >> y;
mp1[y]++;
}
for(auto it=mp1.begin(); it !=mp1.end();it++){
mp2.insert({it->second,it->first});
}
cout << "Case #"<< k << ": Dilbert should drink beverages in this order: ";
for(auto it=mp2.begin(); it!=mp2.end(); it++){
v[c]=it->second;
c++;
}
for(int i=0; i<n; i++){
if(i==n-1){
cout << v[i] <<"."<<endl;
continue;
}
cout << v[i] << " ";
}
}
return 0;
}

How to print in the console that figure?

I want to print that in the console:
*
**
***
**
*
so, my code is:
Scanner input = new Scanner(System.in);
int n = input.nextInt();
char c = '*';
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
any proposals how to finish?
You want to print "* **". Write this:
System.out.print("* **");
If my answer is not that what you excpected, than give us more information. What is your actual problem. See StackOverflowFAQ
If n is your "*" length,below code:
if (1 < n && n < 20) {
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(c);
}
System.out.println();
}
for (int row =1; row <= n; row++) {
for (int col = n-row; col >0 ; col--) {
System.out.print(c);
}
System.out.println();
}
}

cv::matchShapes method implementation giving error

My objective is to find the pattern in the image captured from camera for this I found the cv::matchShapes method of opencv to implement it.
When I have implemented this method it is making the app crash on matchShapes method.Might be I have done something wrong:-
I exactly do not know how to use this method in order to find the match shape in my query image.This is what I tried.
Here is my code:-
- (void)tryingMatchShapes:(cv::Mat)_image _image1:(cv::Mat)_image1
{
std::vector<std::vector<cv::Point> > squares;
cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
int thresh = 50, N = 11;
cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
cv::pyrUp(pyr, timg, _image.size());
std::vector<std::vector<cv::Point> > contours;
for( int c = 0; c < 3; c++ ) {
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
for( int l = 0; l < N; l++ ) {
if( l == 0 ) {
cv::Canny(gray0, gray, 0, thresh, 5);
cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
}
else {
gray = gray0 >= (l+1)*255/N;
}
cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
}
}
cv::Mat pyr1, timg1, gray2(_image1.size(), CV_8U), gray1;
cv::pyrDown(_image1, pyr1, cv::Size(_image1.cols/2, _image1.rows/2));
cv::pyrUp(pyr1, timg1, _image1.size());
std::vector<std::vector<cv::Point> > contours1;
for( int c = 0; c < 3; c++ ) {
int ch[] = {c, 0};
mixChannels(&timg1, 1, &gray2, 1, ch, 1);
for( int l = 0; l < N; l++ ) {
if( l == 0 ) {
cv::Canny(gray2, gray1, 0, thresh, 5);
cv::dilate(gray1, gray1, cv::Mat(), cv::Point(-1,-1));
}
else {
gray1 = gray2 >= (l+1)*255/N;
}
cv::findContours(gray1, contours1, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
}
}
for( size_t i = 0; i < contours.size(); i++ )
{
double value= cv::matchShapes(contours[i], contours1[i], CV_CONTOURS_MATCH_I1, 0);
NSLog(#"%f",value);
}
}
UIImage *testImage = [UIImage imageNamed:#"note_with_marks (1).png"];
[self.imageView setImage:testImage];
cv::Mat forground = [testImage CVMat];
UIImage *testImage2 = [UIImage imageNamed:#"1.png"];
cv::Mat forground2 = [testImage2 CVMat];
[self tryingMatchShapes:forground _image1:forground2];
The app is getting crashed.
Error:-
OpenCV Error: Assertion failed (contour1.checkVector(2) >= 0 && contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth()) in matchShapes, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/imgproc/src/contours.cpp, line 1705
Please some body help me in implementing this method from coding point of view.
Please I need some coding help.I have already gone through I lot of theoretical concepts.
Thanks in advance!
std::vector<std::vector<cv::Point> > contours1;
You can't apply matchShapes to such type. You can apply it for each (one) contour, not for group (array) of contours. For example:
cv::matchShapes(contours[0], contours1[0], CV_CONTOURS_MATCH_I1, 0);

Memory Access Exceedingly Slow For a Simple Loop Through an Array

I am taking about 50 times as long as expected to loop through a simple assignment. My first reaction was that I had disordered my memory access in the arrays, resulting in cache misses. This doesn't seem the case, however.
The pixel value assignment and updating the arrays takes a dogs age. Do any one of you folks have an inclining as to why this is happening? (I am compiling for an iPod with an A4)
memset(columnSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageWidth/numSubdivisions) );
memset(rowSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageHeight/numSubdivisions) );
int pixelValue = 0;
int startingRow = 0;
int startingColumn = 0;
for (int i = 0; i < _validImageHeight/numSubdivisions; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < (_validImageWidth/numSubdivisions); j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}
The result of _validImageWidth/numSubdivisions must be an integer, are you sure that is always the case?
Also, you should calculate _validImageWidth/numSubdivisions before entering the double loops, it's not safe to assume your compiler takes care of it.
int limit = _validImageHeight/numSubdivisions;
for (int i = 0; i < limit; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < limit; j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}

Bottoms-up mergesort problems!

I am having problems with bottoms-up mergesort. I have problems sorting/merging. Current code includes:
public void mergeSort(long[] a, int len) {
long[] temp = new long[a.length];
int length = 1;
while (length < len) {
mergepass(a, temp, length, len);
length *= 2;
}
}
public void mergepass(long[] a, long[] temp, int blocksize, int len) {
int k = 0;
int i = 1;
while(i <= (len/blocksize)){
if(blocksize == 1){break;}
int min = a.length;
for(int j = 0; j < blocksize; j++){
if(a[i*j] < min){
temp[k++] = a[i*j];
count++;
}
else{
temp[k++] = a[(i*j)+1];
count++;
}
}
for(int n = 0; n < this.a.length; n++){
a[n] = temp[n];
}
}
}
Obvious problems:
i is never incremented.
At no point do you compare two elements in the array. (Is that what if(a[i*j] < min) is supposed to be doing? I can't tell.)
Why are you multiplying i and j?
What's this.a.length?
Style problems:
mergeSort() takes len as an argument, even though arrays have an implicit length. To make matters worse, the function also uses a.length and length.
Generally poor variable names.
Nitpicks:
If you're going to make a second array of the same size, it is common to make one the "source" and the other the "destination" and swap them between passes, instead of sorting into a temporary array and copying them back again.