C++ help

THE BLACK GEORGE COSTANZA

CONTENT CREATOR
Registered
hey guys i need some help please. i have to do this for hw Write a program that finds and displays a max number in the array of 10 integer numbers. Also, sort that array in ascending order and display it on the screen. so far this is what i have so far

#include <stdio.h>
#include <iostream>
#include <algorithm>

#define MAX 10

int a[MAX];
int main()
{
int array[]={23,5,-10,0,0,321,1,2,99,30};
int elements=sizeof(array);
std::sort(array,array+elements);
int i,j;

for (i=0; i < MAX; i++)
{
a=i;
printf("%d\n",a);
}

for (j=0;j< MAX-1; j++)
{
printf("please enter number>");
scanf("%d",&a);
}
printf("%d\n");
for (i=0; i < MAX; i++)
printf("%d\n",a);

for (int i=0; i<elements;++i)
std::cout<<array<<' ';
return 0;
}


but when i compile it its still wrong. can someone help me please?
 
My C syntax is bad, If this were PHP I'd would have posted the solution by now.

But what's going wrong? Is the array not returning properly or are you encountering errors?
 
My C syntax is bad, If this were PHP I'd would have posted the solution by now.

But what's going wrong? Is the array not returning properly or are you encountering errors?


i am not getting errors but when it dis play it says 0,1,2,3,4,5,6,7,8,9 then it says please enter number then i do it and then it has an abort error then it closes
 
i am not getting errors but when it dis play it says 0,1,2,3,4,5,6,7,8,9 then it says please enter number then i do it and then it has an abort error then it closes

Find
Code:
printf("please enter number>");

Replace
Code:
printf("please enter number");

See if that works.
 
hey guys i need some help please. i have to do this for hw Write a program that finds and displays a max number in the array of 10 integer numbers. Also, sort that array in ascending order and display it on the screen. so far this is what i have so far

#include <stdio.h>
#include <iostream>
#include <algorithm>

#define MAX 10

int a[MAX];
int main()
{
int array[]={23,5,-10,0,0,321,1,2,99,30};
int elements=sizeof(array);
std::sort(array,array+elements);
int i,j;

for (i=0; i < MAX; i++)
{
a=i;
printf("%d\n",a);
}

for (j=0;j< MAX-1; j++)
{
printf("please enter number>");
scanf("%d",&a);
}
printf("%d\n");
for (i=0; i < MAX; i++)
printf("%d\n",a);

for (int i=0; i<elements;++i)
std::cout<<array<<' ';
return 0;
}


but when i compile it its still wrong. can someone help me please?


I noticed a few more errors, here's what I did:

Code:
#include <stdio.h>
#include <iostream>
#include <algorithm>

#define MAX 10

int a[MAX];
int main()
{
int array[]={23,5,-10,0,0,321,1,2,99,30};
int elements=sizeof(array);
std::sort(array,array+elements);
int i,j;

for (i=0; i < MAX; i++)
{
   a[i]=i;
   printf("%d\n",a[i]);
}

for (j=0;j< MAX-1; j++)
{
   printf("please enter number");
   scanf("%d",&a[i]);
}
   printf("%d\n");

for (i=0; i < MAX; i++)
{
   printf("%d\n",&a[i]);
}

for (int i=0; i<elements;++i)
{
   std::cout<<array[i]<<' ';
   return 0;
}
}
 
ok this is the message i get run-time check failure #2-stack around the variable 'array' was corrupted?



I noticed a few more errors, here's what I did:

Code:
#include <stdio.h>
#include <iostream>
#include <algorithm>

#define MAX 10

int a[MAX];
int main()
{
int array[]={23,5,-10,0,0,321,1,2,99,30};
int elements=sizeof(array);
std::sort(array,array+elements);
int i,j;

for (i=0; i < MAX; i++)
{
   a[i]=i;
   printf("%d\n",a[i]);
}

for (j=0;j< MAX-1; j++)
{
   printf("please enter number");
   scanf("%d",&a[i]);
}
   printf("%d\n");

for (i=0; i < MAX; i++)
{
   printf("%d\n",&a[i]);
}

for (int i=0; i<elements;++i)
{
   std::cout<<array[i]<<' ';
   return 0;
}
}
 
ok this is the message i get run-time check failure #2-stack around the variable 'array' was corrupted?

I noticed you had a lot of opened loops, I thought I closed all of them. Double check you original code (not the one I modified) and check to see if the loops are closed properly. Also check to see if you referenced the variable "a" in all your loops.
 
can you explain that i am sorry. this grad class i am in we only meet once a month and i am so confuse i wish we would of met more then once a month cuz this shit is hard.



I noticed you had a lot of opened loops, I thought I closed all of them. Double check you original code (not the one I modified) and check to see if the loops are closed properly. Also check to see if you referenced the variable "a" in all your loops.
 
can you explain that i am sorry. this grad class i am in we only meet once a month and i am so confuse i wish we would of met more then once a month cuz this shit is hard.

Your "for" loops.

A proper for loop
Code:
for (i > 0; MAX > i; i++)
{
   do somehting;
}

I noticed 2 of your loops were not "opened" and "closed" with curly braces "{" "}"

Code:
for (i=0; i < MAX; i++)
printf("%d\n",a[i]);

for (int i=0; i<elements;++i)
std::cout<<array[i]<<' ';
return 0;
 
hey guys i need some help please. i have to do this for hw Write a program that finds and displays a max number in the array of 10 integer numbers. Also, sort that array in ascending order and display it on the screen. so far this is what i have so far

#include <stdio.h>
#include <iostream>
#include <algorithm>

#define MAX 10

int a[MAX];
//this should probably go inside main()

int main()
{
int array[]={23,5,-10,0,0,321,1,2,99,30};
int elements=sizeof(array);
std::sort(array,array+elements);
//this function is supposed to accept iterators
int i,j;

for (i=0; i < MAX; i++)
{
a=i;
printf("%d\n",a);
}

for (j=0;j< MAX-1; j++)
//this loop should go on while j<MAX otherwise you only get MAX-1 inputs
{
printf("please enter number>");
scanf("%d",&a);
}

printf("%d\n");
//what is this supposed to print?

for (i=0; i < MAX; i++)
printf("%d\n",a);
//tries to print more numbers than were defined

for (int i=0; i<elements;++i)
//use braces with loops, it makes it easier to read
std::cout<<array<<' ';
//might print only nine of the ten numbers

//placing a space here makes it easier to read
return 0;
}


but when i compile it its still wrong. can someone help me please?


I didn't bother compiling it but these few things jumped out at me
 
i did it and still getting that error


Your "for" loops.

A proper for loop
Code:
for (i > 0; MAX > i; i++)
{
   do somehting;
}
I noticed 2 of your loops were not "opened" and "closed" with curly braces "{" "}"

Code:
for (i=0; i < MAX; i++)
printf("%d\n",a[i]);

for (int i=0; i<elements;++i)
std::cout<<array[i]<<' ';
return 0;
 
Here's my shot at this. It compiles and works.

//Max Integer Example by Player+

#include <stdafx.h>
#include <iostream>

using namespace std;

int main()

{

const int MAX_NUM_INTEGERS = 10;
long int integers[10];
long int i = 0, maximumValue = 0;

cout << "Please enter 10 integers: \n\n";
while ( i < MAX_NUM_INTEGERS)

{

cin >> integers;
i++;

if (integers[i - 1] > maximumValue)
{

maximumValue = integers[i-1];

}
}

cout << "\nThe largest number entered was " <<maximumValue << "\n\n";
system("pause");

return 0;
}
 
Last edited:
how long you been programming.....

any tips for arrays and pointers.......I hate them.....lol

I've been programming for a few years. I started out as a hardware cat, then bent into networking and server systems. Took a break from that and dipped into design (web, graphics, imaging). Now, I do it all again. I love programming because I get to make something out of nothing.

Tips:

* Try to re-use memory if possible because allocating and de-allocating has an associated overhead
* A variable can be declared static, to retain its value between function calls
* Memory should be de-allocated before a pointer exits scope
* Accessing a de-allocated object (or using an uninitialized pointer) is unsafe, since the memory is not in your control
 
Last edited:
Do any of you know how to do this......?



Give a program shown below, type it in and execute it. Make sure you understand what it does.


#include <iostream>
#include <ctime>
#include <string>

using namespace std;

int main ()
{
time_t start,end;
string name;
double dif;

time (&start);
cout << "Please, enter your name: ";
cin >> name;
time (&end);
dif = difftime (end,start);
cout << "Hi " << name << endl;
cout << "It took you " << dif << " seconds to type your name" << endl;

return 0;
}


Use some of its statements to measure the execution times of a program with O(n) when the input is n =100, 1000 ,10000 and 100000

Then measure the execution times of a program with O(2n) when the input is
n=5, 10, 15, 20, 25, 30

Quit when it runs longer than 2 hours

Use a table to summarize the executions of all the runs(convert seconds to minutes or hours when appropriate)

Remarks:


Declare all the variables in your program as double to avoid the problem of overflow
 
Back
Top