Permalänk
Medlem

vad är fel?

//Försöker lägga in värden i en array och sen stega igenom dem med en pekare och sen räkna ut sammanlagda värdet av alla tal, minsta talet och största talet i arrayen.

#include <iostream> // cout, cin
#include <ctime>
#include <string>
#include <stdio.h>
using namespace std;

int main()
{
//Initiera programmet
dos_console(); //Kolla om detta behövs
srand((unsigned)time(0)); //Kolla om detta behövs

int size=0;
cout << "Ange arraystorlek: ";
cin >> size;
int array[size];

for (int i = 0; i<size;i++)
{
array[i]= rand()%100000/100.0; // Slumpa ett flyttal i intervallet 0 till 100 (100 är inte med!)
int summa = summa + array[i] //beräknar sammanlagda värdet
cout << array[i] <<endl;
}

int c=0;
//Beräkna största värdet
for (int *counterpekare = array[0]; c<array[size]; c++) //OBS, int c funkar?
{

if (array[c] > counterpekare)
counterpekare = array[c];

}

int c2=0;
//Beräkna minsta värdet
for (int *counterpekare2 = array[0]; c2<array[size]; c2++) //OBS, int c funkar?
{
if (counterpekare2 < array[c2])
counterpekare2 = array[c2];
}
}

Permalänk
Medlem

Re: vad är fel?

Citat:

Ursprungligen inskrivet av MacDaddy

//Försöker lägga in värden i en array och sen stega igenom dem med en pekare och sen räkna ut sammanlagda värdet av alla tal, minsta talet och största talet i arrayen.

Jaha? Och vad är det som inte fungerar? Går koden inte att kompilera, eller blir det problem under körningen? Om du vill ha hjälp så får du beskriva ditt problem, och inte bara slänga upp lite kod och förvänta dig att någon ska orka gå igenom den för att hitta ditt fel. Nu gör du förvisso ett ganska uppenbart fel, så jag ska vara snäll och hjälpa dig ändå.

Citat:

Ursprungligen inskrivet av MacDaddy

int size=0;
cout << "Ange arraystorlek: ";
cin >> size;
int array[size];

Du får inte deklarera en array med en vid kompileringstillfället okänt storlek. Om du vill ha en array med dynamisk storlek så får du antingen allokera den själv med new[], eller använda standardbehållaren vector.

Permalänk

Om inte annat är ditt trådnamn fel och du saknar fråga

Visa signatur

Asus Striker II Extreme / XFX Geforce GTX 280 / Q9450 @ 3.6GHz/ TRUE Noctua 120/ 4x1GB Corsair TWIN3X2048-1333C9DHX / X25-M G2 80gb Velociraptor / Win 7 Ultimate x64/ Antec P190

MovieDatabase

Permalänk
Medlem

Du menar sista raden blir:
int array[] = new int[size];

Btw, här är felmeddelandet:
1>------ Build started: Project: Labben1-Pointer, Configuration: Debug Win32 ------
1>Compiling...
1>main.c
1>e:\skola\skola - jönköping\sommarkurser\objektbaserad programmering\alla_labbar\labbkod\labben1-pointer\labben1-pointer\main.c(1) : error C2059: syntax error : '/'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(25) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(26) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(27) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(28) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(29) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(30) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(31) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(32) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(33) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(33) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(33) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(33) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(34) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(34) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(34) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(34) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(35) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(36) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(37) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(38) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(39) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(40) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdio(40) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\stdlib.h(239) : error C2081: 'uintptr_t' : name in formal parameter list illegal
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(18) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : error C2059: syntax error : ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : error C2143: syntax error : missing '{' before ':'
1>d:\program\programmering\microsoft visual studio 8\vc\include\cstdlib(20) : fatal error C1003: error count exceeds 100; stopping compilation
1>Build log was saved at "file://e:\Skola\Skola - Jönköping\Sommarkurser\Objektbaserad programmering\alla_labbar\LabbKod\Labben1-Pointer\Labben1-Pointer\Debug\BuildLog.htm"
1>Labben1-Pointer - 102 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Permalänk
Medlem

int summa = summa + array[i]
saknar ;

använd [ code] [ /code] för kod.

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Micket
int summa = summa + array[i]
saknar ;

använd [ code] [ /code] för kod.

Det där har jag aldrig sett.

Permalänk
Medlem

Re: vad är fel?

Citat:

int c=0;
//Beräkna största värdet
[B]for (int *counterpekare = array[0]; c<array[size];[/B] c++) //OBS, int c funkar?
{
[/B]

Den rader ger felmeddelandet: error C2440: 'initializing' : cannot convert from 'int' to 'int *'

Betyder det?

Permalänk
Medlem

Använd code-taggen!

Felmeddelandet är att du blandar typerna.
array är en lista av int
array[0] är en int.
Du försöker sätta counterpekare (som är en pekare till en int) till värdet av en int.

hela raden "for (int *counterpekare = array[0]; c<array[size]; c++)" är totalt felaktig.
Vad vill du göra?

array[size] är ogiltligt. array[x] ger VÄRDET i listan på position x. size är hela längden på listan och blir då utanför listans längd. (en lista av storlek 10 innehåller index från 0 till 9)
Använd inte så kryptiska namn. counterpekare? Vad fan betyder det?

int max = array[0]; for (int i = 1; i < size; i++) { if (array[i] > max) { max = array[i]; } }

Permalänk
Medlem

php taggen är mer soft

int max = array[0]; for (int i = 1; i < size; i++) { if (array[i] > max) { max = array[i]; } }

[php][ /php]

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Tino
php taggen är mer soft

Men den är, som namnet antyder, till för PHP.
Swec borde implementera någon annan syntax highlighting, typ syntaxhighlighter. Då skulle man kunna färglägga språk som inte har C-syntax också.

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av You
Men den är, som namnet antyder, till för PHP.
Swec borde implementera någon annan syntax highlighting, typ syntaxhighlighter. Då skulle man kunna färglägga språk som inte har C-syntax också.

+1

Något för feedback-forumet kanske?

Permalänk
Medlem

Jag är nöjd om det slutar vara smileys i koden

:(

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Micket
Jag är nöjd om det slutar vara smileys i koden

:(

Finns en liten checkbox för det när du postar, men håller med om att den borde låta bli med smilies inanför kodtaggarna.

Permalänk
Medlem

Jag vet. Men den saknas även vid snabbsvar.

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Micket
Jag vet. Men den saknas även vid snabbsvar.

Gör den?

Permalänk
Medlem
Permalänk
Medlem

Det är rätt bra gjort att skriva i snabbsvarsrutan att det saknas en kryssruta som ligger precis under den

Det jag tycker är mest akut är syntax highlighting för T-SQL, brukar bli rätt trista queries för ögonen att rota i när man ska hjälpa någon. Skulle vara trevligt med knappar i editorn för de tillgängliga taggar som finns, eller en lättillgänglig referens iaf som man kan titta på.

Detsamma gäller smileys, naturligtvis ska där finnas en referenstabell över dem.

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av azoapes
Skulle vara trevligt med knappar i editorn för de tillgängliga taggar som finns, eller en lättillgänglig referens iaf som man kan titta på.

Detsamma gäller smileys, naturligtvis ska där finnas en referenstabell över dem.

Ärsh, det är vädligt enkelt att hitta dem.

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av cic
Ärsh, det är vädligt enkelt att hitta dem.

Om de ska gå att hitta överhuvudtaget, ska det vara via Skriv svar/Ändra/Ny tråd. Men det gör de inte.