c++ simple pointer program [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is a simple program where it sums all of the numbers sent in on the command line. It should take an arbitrary number of values.
It keeps giving me a 0 for each line. I've tired to make several changes but it continues to give the same output
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
{
int sum=0;
sum+=atoi("argc[i]");
cout << sum << endl;
}
return 0;
}

"argc[i]" is a literal string so when converted by atoi gives 0! sum should be initialized before the loop :
int main(int argc, char *argv[]) {
int sum=0;
for (int i = 1; i < argc; i++) {
sum += atoi(argv[i]);
}
cout << sum << endl;
return 0;
}
Conventionally argv[0] is the name of the program (or at least the name used in the command line to invoke the program), so better start at index 1.
You need to spend dozen of hours reading more your books and experimenting on your computer. Asking here such a basic question don't help you at all (and is considered as rude...).
Don't forget to enable debugging info and all warnings when compiling (e.g. with g++ -Wall -g if using GCC). Then, learn how to use the debugger (e.g. gdb).

Your basics are not clear, I suggest you to read the book.
Program should be like this:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int sum=0;
for (int i = 0; i < argc; i++)
{
sum+=atoi(argv[i]);
}
cout << sum << endl;
return 0;
}

move parameter sum outside loop!!
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int sum=0;
for (int i = 0; i < argc; i++)
{
sum+=atoi(argv[i]);
cout << sum << endl;
}
return 0;
}

Related

string to integers from argv

I've a program which is taking input from command-line and using them ahead. All this time the inputs were given separately after the invocation command eg ./a.out 1 2 3 4 5 and it was quite easy to use them (lets just sum them for the moment) -
#include <iostream>
#include <cstdlib>
int main(int argc, char **argv) {
int sum = 0;
for(int i = 1; i < argc; ++i) {
sum += std::atoi(argv[i]);
}
std::cout << sum << std::endl;
}
but now the input format has changed to ./a.out "1 2 3 4 5" and this method does not works correctly. I tried to take "1 2 3 4 5" to a string but then I cannot get the integers out of it. Nor can I think of any other solution at this moment. Is there any elegant method to take them out of argv without too much complexity?
You need to split the string. You can do this for example using string streams:
#include <iostream>
#include <sstream>
int main(int argc, char **argv) {
int sum = 0;
for(int i = 1; i < argc; ++i) {
std::istringstream iss(argv[i]);
for (int n; iss >> n;) sum += n;
}
std::cout << sum << std::endl;
}

How do you use nested for loops to make triangles in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here's what I have:
for (int i = 0 ; i <= height; i++) {
for (int s = 0; s < height; s++) {
cout << "*" << endl;
}
}
It prints out a list of asterisks like this:
*
*
*
*
You're outputting a newline after every single character. You only want to do that in the outer loop, after each line of characters.
In addition, using a fixed value for the width will result in a square rather than a triangle. You should therefore base your inner loop on i rather than height:
#include <iostream>
using namespace std;
int main (void) {
int height = 7;
for (int i = 1 ; i <= height; i++) {
for (int s = 0; s < i; s++)
cout << "*";
cout << endl;
}
return 0;
}
The output of that is the very triangular:
*
**
***
****
*****
******
*******
Of course, that will work but it's hardly embracing the essence of the C++ language.
What you really have there is C code but using C++ output methods, something that will get you quickly labeled as a C+ programmer :-)
Although I wouldn't hand this in as a beginner assignment, it will at least force you to think about using the added facilities of the C++ language going forward:
#include <iostream>
using namespace std;
int main (void) {
int height = 7;
string s(height, '*');
for (int i = 1 ; i <= height; i++)
cout << s.substr(0,i) << '\n';
return 0;
}
That thinking will convince you at some point to discard character arrays for strings (or arrays in general for vectors, or many other similar decisions), something that will make your programming much faster and safer in future.
try this
int main()
{
int height =10;
for (int i = 0 ; i <= height; i++) {
for (int s = 0; s < i; s++) {
cout << "*";
}
cout << endl;
}
}

Windows compilation error when running a small program

When I ran my solution to The Square Within from codercharts.com on my computer (Intel i5 newest version), I was able to build my program in 646 ms, but for some random reason I was able to enter the code for the input part of my code, but when I pressed "enter" to get results, the screen popped up with a Windows error:
Here is my code that I ran (logic all follows the problem I was given).
#include <iostream>
using namespace std;
int main (int argc, char ** argv)
{
int dimension[]={};
int result;
int counter = 0;
for (int i=0; i < 1000000; i++){
counter+=1;
}
for (int a=0; a<counter; a++){
result=(dimension[a]*(dimension[a] + 1)*((2*dimension[a]) + 1))/6;
}
while (true){
cin >> dimension[counter];
break;
cout << result << " ";
}
return 0;
}
Can something review my code and help me with this hard problem? Thanks in advance!
The line
int dimension[]={};
needs a value for its size:
i.e.
int dimension[1000001]={};

Filling an 2D array with pseudo-random numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm new to C++ and trying to populate and output a 12x12 array using pseudo-random numbers.
Can anyone see where the code is failing?
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <random>
using namespace std;
const int i = 12;
const int j = 12;
int myArray [i] [j] = {0};
void generateArray();
int main()
{
generateArray();
return 0;
}
void generateArray()
{
srand(1234);
for(int i=0; i < 12; i++);
{
for(int j=0; j < 12; j++);
{
myArray[i][j]= rand();
}
cout << myArray[i][j] << " ";
}
}
Thanks,
Ryan
Your for loops have semicolons after their closing parentheses.
This will effectively treat the for loop as an empty body (and just ignore the loop in general).
Remove them and the code should execute.
Your for loop should look as below.
for(int i=0; i < 12; ++i)
{
for(int j=0; j < 12; ++j)
{
myArray[i][j]= rand();
cout << myArray[i][j] << " ";
}
cout << endl;
}

Whats wrong with the following code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have written down a code in C++ to make the words appear in array from a string input.
But it doesn't works due to overflow or something. Compiler doesn't show any error though.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
for(int j=0;j<strlen(a);j++) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j];
k++;
}
while(a[j]!=' ');
I++;
}
cout<<b[0][0];
return 0;
}
If you're going to use C strings, you need to add a null terminator at the end of each string
do {
b[I][k]=a[j];
k++;
} while(j+k<strlen(a) && a[j+k]!=' ');
b[I][k] = '\0';
As ppeterka noted, you also need to change your loop exit condition to avoid an infinite loop.
Note also that the repeated calls to strlen here and in your code are wasteful. You should consider calculating this once before your for loop instead.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
int j =0;
while(j< 100) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j+k];
k++;
} while(a[j+k]!=' ');
b[I][k+1] = '/0';
I++;
j= j+k;
}
cout<<b[0][0];
return 0;
}