cplusplus.com
C++ : Forum : Beginners : Where is the problem?
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


post Where is the problem?

murmuls404 (25)
This program dont work.
If there is not even number after odd number,all negative numbers must be printed out from the end to beginning,otherwise positive.



#include<iostream>
using namespace std;

int sk;
int a;
bool skaitli = true;
int main()


{
cout<<"How many integers you want to input?"<<endl;
cin>>sk;
int arr[sk];
for (int i=0;i<sk;i++)
{cout<<"Enter "<<i+1<<". string number "<<endl;
cin>>arr[i];
}

for (int j=0;j<sk;j++)
{ if (arr[j]%2!=0&&arr[j+1]%2==0)
skaitli=true;
else
skaitli=false;
}

if (skaitli)
{
a=sk-1;
for (a;a==0;a--)
if (arr[a]<0) cout<<arr[a];
else
if (arr[a]>0) cout<<arr[a];
}
system("pause");
return 0;
}
computerquip (1682)
Next time, please format your code to make it readable and use the code bbcodes please.
Standard C++ formatting with 3 space tabs.

To get to the point: So there must be a postive number, after a negative number to print all evens else it prints odds? May I ask the purpose of this program without looking at the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
using namespace std;

int sk;
int a;
bool skaitli = true;
int main()
{
   cout<<"How many integers you want to input?"<<endl;
   cin>>sk;
   int arr[sk];
   for (int i=0;i<sk;i++)
   {
      cout<<"Enter "<<i+1<<". string number "<<endl;
      cin>>arr[i];
   }

   for (int j=0;j<sk;j++)
   { 
      if (arr[j]%2!=0&&arr[j+1]%2==0)
         skaitli=true;
      else
         skaitli=false;
   }

   if (skaitli)
   { 
      a=sk-1;
      for (a;a==0;a--)
      {
         if (arr[a]<0) 
            cout<<arr[a];
         else 
            if (arr[a]>0) cout<<arr[a];
      }
   }
   system("pause");
   return 0;
}
Last edited on
wasabi (207)
Well, one thing that's wrong is on line 11: int arr[sk];

You're not allowed to do that. You need to use either malloc ( int* arr = malloc(sk*sizeof(int));) or new (int* arr = new int[sk]). However, you then need to free(arr) (in the case of malloc) or delete[] arr; (in the case of new) to prevent any possible memory leaks.

I haven't run this, though, so I don't know if there's anything else wrong. It's just the first thing I noticed.
murmuls404 (25)
to computerquip

if user inputs: 2 -2 3 -5 5 2 69 -63 i have to print out 69 2 5 3 2
if user inputs 2 4 6 8 -10 8 6 -12 i have to print out -12 -10

No matter whether there is positive after negative or negative after positive.
I have to know if there is even after odd.
like this arr[j]%2!=0&&arr[j+1]%2==0
murmuls404 (25)
Still no one can help?:( Made a little changes.And have no idea how to make readeble with bbcode.Completily stupid..

#include<iostream>
using namespace std;


int main()


{
int sk;
int *arr;
bool paris = true;

cout<<"Cik skaitlus velies ievadit?"<<endl;
cin>>sk;
arr = new int[sk];
for (int i=0;i<sk;i++)
{cout<<"Ievadi "<<i+1<<". skaitli "<<endl;
cin>>arr[i];
}

for (int j=0;j<sk-1;j++)
{ if ((arr[j]%2!=0)&&(arr[j+1]%2==0))
paris=true;
else
paris=false;
}

if (paris)
{
int a=sk-1;
for (a;a<0;a--)
if (arr[a]<0) cout<<arr[a];
else
if (arr[a]>0) cout<<arr[a];
}
delete[] arr;
cin >> sk;
system("pause");
return 0;
}
chwsks (194)
is this what you want?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;

void keep_window_open(){
	char ch; 
	cout<<"enter any character to continue..."; 
	cin>>ch;
}

int main(){
	int sk;
	int *arr;
	bool paris = true;

	cout<<"How many integers you want to input?"<<endl;
	cin>>sk;
	arr = new int[sk];
	for (int i=0;i<sk;i++){
		cout<<"Enter "<<i+1<<". string number "<<endl;
		cin>>arr[i];
	}

	for (int j=0;j<sk;j++){
 
		if ((arr[j]%2!=0)&&(arr[j+1]%2==0)){
			paris=true;
			break;
		}
		else paris=false;
	}

	if (!paris){
		for (int a=sk-1;a>=0;a--){
			if (arr[a]<0)cout<<arr[a]<<' ';
		}
	}
			
	if (paris){
		for (int a=sk-1;a>=0;a--){
			if (arr[a]>0)cout<<arr[a]<<' ';
		}
	}

	delete[] arr;
	keep_window_open();
	return 0;
}
gcampton (859)
[c o d e ] code goes in here then you finish with [/c o d e ]

Also you should use more meaningful variable names.
Last edited on
Topic archived. No new replies allowed.