I just want to ask your help here. I am new beginner in c++ programming. How to print out the even number in range 100 - 200. I tried write some code and it didn't work out. Here is my code. I hope, someone here can help me. Will appreciate that so much. Thanks.
include <stdio.h>
void main()
{
int i;
for (i= 100; i<= 200; i += 2){
print i;
}
}
Well, pretty simple:
#include <iostream> // This is the C++ I/O header, has basic functions like output an input.
int main(){ // the main function is generally an int, not a void.
for(int i = 100; i <= 200; i+=2){ // for loop to advance by 2.
std::cout << i << std::endl; // print out the number and go to next line, std:: is a prefix used for functions in the std namespace.
} // End for loop
return 0; // Return int function
} // Close the int function, end of program
you were using C libraries, not C++ ones, as well as no function that is called print in C++, nor C. Also there is no void main function, use int main() instead. finally, you need to have std:: in front of cout and endl as these lie in the std namespace.
Use the following code:
#include <iostream>
int main()
{
int i;
for (i= 100; i<= 200; i += 2){
std::cout << i << std::endl;
}
return 0;
}
Your code looks good....Only the printing part needs to be changed
#include <stdio.h>
int main()
{
for (int i= 100; i<= 200; i += 2){
printf("%d",i);
}
return 0;
}
This might help!
#include <iostream>
using namespace std;
int main()
{
for (int count = 100; count <= 200; count += 2)
{
cout << count << ", ";
}
cout << endl;
return 0;
}
Related
I want to preface this by saying this is the first assignment in a 102 csc course. My 101 teacher wasn't the best, so I don't have that good of a foundation.
I have an assignment which requires every action to be in its own function. I have to generate random numbers, then write these numbers to a file, and then a few other things. I have the file created and written to. However, I am having a problem where I can't read the values from the file into an array. They are all still 0 when output to the console. I am sure it is something I'm doing wrong with the functions, but I have no problem taking any criticism. I will include the code below. The function I am working in now is named void read().
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <cstddef>
using namespace std;
fstream randomData;
void randomgenerator();
void read(fstream &randomdata);
int main() {
randomgenerator();
read(randomData);
return 0;
}
void randomgenerator() {
srand(time(0));
randomData.open("randomData.txt");
for (int counter = 0; counter < 100; counter++) {
randomData << rand() % 100+1 << endl;
}
}
void read(fstream &randomData) {
int numarray[100] = {};
for (int i = 0; i < 100; i++) {
randomData >> numarray[i];
cout << numarray[i];
}
}
Thank you for taking the time to give input. The std namespace is used because the professor wants us to. I understand it is not efficient.
This was the solution for me, hope this helps any future searches for this problem. The solution was to create another variable with datatype ifstream. This variable accessed the same file, but allowed me to read from it instead of writing to it. I had to close the file from the randomData variable, and open the inputrandomData variable. Below is the revised code.
using namespace std;
ofstream randomData;
ifstream inputrandomData;
void randomgenerator();
void read();
int main() {
randomgenerator();
read();
return 0;
}
void randomgenerator() {
srand(time(0));
randomData.open("randomData.txt");
for (int counter = 0; counter < 100; counter++) {
randomData << rand() % 100+1 << endl;
}
randomData.close();
}
void read() {
inputrandomData.open("randomData.txt");
int numarray[100] = {};
for (int i = 0; i < 100; i++) {
inputrandomData >> numarray[i];
cout << numarray[i];
}
}
Thanks for the assistance folks!
I know that you probably gona again vote me down, I really don't understand this but im really stuck at something and cant figure it out , there is no such information anywhere in the web , neither in my book for the course, so I have this assignment where I need make 2 sums of containers where the difference between 2 sums is the lowest , so the program is done is working perfectly calculated everything however , in my assignment:
The user enter on one row unkwonw length numbers so after that I do all kind of sums between them and find the one with lowest difference between.
Ok but the way I wrote the code I use one while(true) so that to work with infinity testcases(as from assignment) and in this while(true) I have another while(cin>>(SOMEINT)) loop and push it back in a vector , and after it reads new line it just break the wile and continue with the calculation.
However in our test software this one give runtime error since after finding some cases then it start print infinity 0 0 since there is nothing to enter but the while(true) just continues.
I mean I just want to make it that way that the while is till user enters something , for instance you enter 30 50 90 it will return 80 90 , then wiat for another entry and so on.
CODE:
#include <iostream>
#include <string>
#include<vector>
#include <sstream>
#include <cmath>
#include <string.h>
#include <stdio.h>
#include <climits>
using namespace std;
const int length = 17000;
int power(int x){
int sum =2;
for(int i = 0;i<x;i++) {
sum *= 2;
}
return sum;
}
bool ison(int i,int x)
{
if((i>>x) & 1)return true;
return false;
}
int main()
{
while(true){
vector<int> Vec;
int cur = 0;
while (cin >> cur) {
Vec.push_back(cur);
if (cin.get() == '\n') {
break;
}
}
int * sumOfarr1 = new int[length];
int * sumOfarr2 = new int[length];
for(int i = 0; i<length;i++){
sumOfarr1[i] = 0;
}
for(int i = 0; i<length;i++){
sumOfarr2[i] = 0;
}
int index=0;
for(int i=1;i<length;i++)
{
for(int j=0;j<Vec.size();j++)
{
if(ison(i,j))
{
sumOfarr1[index]+=Vec[j];
}
else
{
sumOfarr2[index]+=Vec[j];
}
}index++;
}
int ans=INT_MAX;
int ii;
for(int i=0;i<index;i++)
{
if(abs(sumOfarr1[i]-sumOfarr2[i])<ans)
{
ii=i;
ans=abs(sumOfarr1[i]-sumOfarr2[i]);
}
}
if(sumOfarr1[ii]<sumOfarr2[ii]){
cout << sumOfarr1[ii] << " " << sumOfarr2[ii];
}
else{
cout << sumOfarr2[ii] << " " << sumOfarr1[ii];
}
cout << endl;
delete[] sumOfarr1;
delete[] sumOfarr2;
Vec.clear();
}
return 0;
}
Yes I found the solution just using getline and stringstream.
aka this
vector<int> Vec;
string line;
while(getline( cin, line ))
{
istringstream iss( line );
int number;
while( iss >> number )
Vec.push_back(number);
}
what would be the code to continue counting with a for loop? like if i wanted put in a char and have it print once on the first line, twice on the second line, third on the third line and so on?
I've tried
for (int a; a<5; a++)
cout "L";
but this only prints one L per line
i need it to be more like
L
LL
LLL
Based to the #Saleem answer, here is a complete sample:
#include <iostream>
using namespace std;
int main()
{
for(auto i=0;i<=5;++i)
{
string s(i,'L');
cout << s.c_str() << endl;
}
return (0);
}
You need an additional loop if you do not want to use something like std::string. The inner loop will add L with it's respective amount per line to the output. Once all the amount of Ls are written, you end the line.
Something like:
int main() {
for (int a = 1; a < 5; a++)
{
for (int l = 1; l <= a; l++)
{
std::cout << "L";
}
std::cout << std::endl;
}
return 1;
}
You can use string to repeat character desired number of times.
e.g.
for(auto i=0;i<=5;++i)
{
string s(i,'L');
cout<<s<<endl;
}
This will print:
L
LL
LLL
LLLL
LLLLL
Update:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
for(auto i=0;i<=5;++i)
{
string s(i,'L');
cout<<s<<endl;
}
return 0;
}
Make sure you are using modern c++ compiler.
This is probably a very easy problem, but I am studying while loops and I am trying to write a program that sums numbers from 50 to 100. This is my code that I wrote
#include <iostream>
using namespace std;
int main() {
int sum=0;
int val=1;
while(50 <= val <= 100) {
sum = sum + val;
val = val + 1;
}
cout << "Sum is: " << sum << endl;
return 0;
}
I was to compile the code and get a program, but each time I am trying to run the program on terminal is just goes idle. Is there something wrond with my code? Thanks!
All the comments are valid. Please look at the C++ reference for syntax and how to use operators and loop.
I believe looking at some correct code is also a way to learn and hence posting this :
#include <iostream>
using namespace std;
int main ()
{
int sum = 0;
int i = 50; // Why not start from 50 itself, when you want sum(50-100)
while (i <=100)
{
sum += i; // Same as sum = sum + i
i++; // Same as i = i + 1
}
cout<<sum<<"\n";
return 0;
}
I'm a beginner in c++ and I'm getting two errors in my code and I don't know how to fix them...
the first one
illegal indirection
and the second one is
'=' left operand must be a I-value. (in the line: ((ArrayPtr +i)+j)=rand()%55+1 )
Does anyone have an idea how to fix them? That's my code:
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
printing(Array);
system("PAUSE");
return 0;
}
void FillingRandomly(int *ArrayPtr)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
*(*(ArrayPtr +i)+j)=rand()%55+1;
}
}
}
void printing(int *Array)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS*AS;j++)
{
int counter = 0;
cout<<((Array[i] +j))<<setw(5);
if ((Array[i] +j)%AS == 0)
cout << endl << endl;
}
}
}
void forsorting(int *Brray, int funny)
{
int dice = 0;
int super = 0;
int space=0;
//Sorting Array[][] which is treated like Array[]
{
for (int pass = 0; pass < AS - 1; pass++) {
for (int k = 0; k < AS - 1; k++) {
int temp;
if(*(Brray+k)==*(Brray+k+1))
{
temp=*(Brray+k);
*(Brray+k)=*(Brray+k+1);
*(Brray+k+1)=temp;
}
}
}
}
}
By
*(*(ArrayPtr +i)+j)=rand()%55+1;
it seems you want
ArrayPtr[i][j] = (rand() % 55) + 1;
You can try something along the line of
int const offset = AS * i + j;
int const elem = (rand() % 55) + 1;
*(ArrayPtr + offset) = elem;
Your function signature is:
void FillingRandomly(int *ArrayPtr)
where you are telling to compiler that you are passing a simple pointer, but in the line:
*(*(ArrayPtr +i)+j)=rand()%55+1;
you are doing a double derreference, which is illegal and causing the compiler to complain
COMPLEMENT
I was seeing the comments in the other answer and, as what I need to write is bigger than the reserved commentary space, I decided to complement my own answer.
You defined Array as:
int Array[AS][AS];
Indeed, what you are doing is a promise to compiler that you will use Array as defined, but the compiler doesn't believe in you too much, so that any time you use Array the compiler will make sure that it is being used as declared.
The problem arises when you declare your FillingRandomly function. Here you are broking your promise and are trying to use Array by declaring a differente type. Note how you declare your function:
void FillingRandomly(int *ArrayPtr)
Due the fact that c++ supports function overloading, the compiler doesn't warn you until it initiate the linking phase, when it is unable to find a function whose signature is:
void FillingRandomly(int ArrayPtr[][AS])
note that both are different.
Once you are a beginner, the best way to keep your programs correctly is to keep your promise immutable. Bellow I show you a piece of your own code, correcting those issues for FillingRandomly function (you have to correct it for the others functions too):
const int AS = 6;
void FillingRandomly(int [][AS]); // Note that I've changed your prototype here
....
void FillingRandomly(int ArrayPtr[][AS]) // Keep your function signature the same as your prototype signature
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
ArrayPtr[i][j]=rand()%55+1; // Note how ArrayPtr is being used exactly as your promised early
}
}
}