2013-11-04 30 views
-5

再次抱歉,我在編程方面並不是什麼天才。儘管用戶輸入,我的數組仍然是隨機值

首先總結一下:數組輸入。兩個3D矢量......哈哈,讓我們用矢量來計算MORE矢量。無論如何:點積只是普通的可笑(小數點前9個數字;我的意思是,我從未想過1x8 + 7x5 + 4x2可能有9個數字)。 Cross產品......更糟糕。

我做了......呃......我怎麼稱呼它?那麼,我們稱之爲「traza」。爲了理解,我將翻譯一個定義:代碼的「traza」告訴我們它的執行指令的順序,以及在每行代碼後變量是如何變化的。你知道,包含變量和數字標記的表格引用了代碼行,我們看看代碼是否在做一些意想不到的事情。切入點:就我所能看到的而言,這一切都很好。

然後,我用一個打印命令和向量中的每個值做了一個意想不到的「pseudotraza」。在輸入之後和te dot產品之前(在一個函數中)。猜猜是什麼: 1)它不是我的輸入,在它們中的任何一箇中 2)它們甚至不是相同的值 3)第一個值遠離我的輸入,但以下至少是更多的邏輯(與輸入)。

我在12小時前學會了今天早上使用數組/矢量/任何東西。我從來沒有任何需要在其輸入前將任何值設置爲0。但這是我之前發生的事情之前我所知道的唯一一件事。 (總有一天你們會成爲我的程序設計老師,你對我的瞭解比自己更多......並且原諒我糟糕的語法,西班牙語的英語教學只是「在上一個高中學習一些語法規則並且不超過50次練習......這一切,你需要通過大學入學考試!「)

#include <iostream> 
using namespace std; 
#include <stdlib.h> 

const int N = 3; 
typedef int Vector[N]; 

void introduirVector (Vector); 
float producteEscalar (const Vector, const Vector); /*Input vector and dot product*/ 

int main (void) 
{ 
Vector v1, v2; 
float p_esc; 

cout << "Introduim les dades del vector A: "; /* Input */ 
introduirVector (v1); 

cout << "Introduim les dades del vector B: "; /* 2x Input combo */ 
introduirVector (v2); 

cout << v1[0] << "\n" << v1[1] << "\n" << v1[2] << "\n" << v2[0] << "\n" << v2[1] << "\n" << v2[2] << endl; /* "Puseudotraza*/ 

p_esc= producteEscalar (v1, v2); /*Dot product*/ 

cout << "El producte escalar: " << p_esc; /*Dot product is...*/ 

system ("PAUSE"); 
return 0; 
} 

/*3x Input combo*/ 
void introduirVector (Vector) 
{ 
    int i; 
    Vector v; 

    for (i = 0; i < N; i++) 
    { 
    cin >> v[i]; 
    } 

    return; 
} 

/* Dot product (why all the Vectors are set as constants but another after this is not set? It's the hint given by the teacher, my (not) beloved teacher...they gave us the main function and the other function's prototypes, but that's all) */ 
float producteEscalar (const Vector, const Vector) 
{ 
    float escalar; 
    Vector v1, v2; 
/* Pseudotrazas for all*/ 
    cout << v1[0] << "\n" << v1[1] << "\n" << v1[2] << "\n" << v2[0] << "\n" << v2[1] << "\n" << v2[2] << endl; 

/* Dot product and all that */ 
    escalar = (v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]); 

    return escalar; 
} 
+6

如果您可以將聊天聊天減少到最低限度,那麼這個問題會更具可讀性。只是事實。 –

+0

另外,這裏沒有實際的問題,我可以看到。 –

+0

在introduirVector()和producteEscalar()中,使用局部變量而不是傳遞給函數的數據。但是,接下來你遇到的問題是你通過價值而不是參考傳遞,所以你的介紹矢量無法改變傳遞給它的矢量。 – Ross

回答

2

問題是這樣的:

/*3x Input combo*/ 
void introduirVector (Vector) 
{ 
    int i; 
    Vector v; 

此功能需要一個Vector作爲參數,但矢量沒有名字,因此它不能在函數內部使用。

然後您聲明一個新的本地Vector v。您將用戶的輸入讀入此矢量。

然後該函數結束,此時讀取用戶輸入的向量消失。

首先,你應該使用你被調用的參數,而不是局部變量。但是你的第二個問題是你正在使用pass by value。當你調用這個函數時,introduirVector (v1);它是而不是你知道並且主要關注你在introduirVector裏面工作的「v1」,但是當你的函數返回時,它的一個本地副本不再存在。

你需要做的是讓你的函數接受一個指針或到Vector的引用現在被稱爲有:

void introduirVector(Vector& v) 
{ 
    for (size_t i = 0; i < 3; ++i) { 
     cin >> v[i]; 
    } 
} 

這並不能完全解決所有的問題與您的代碼,但它應該讓你前進。

0

我沒有看到任何投入,這將是你爲什麼不接受任何輸入。

嘗試使用:

string inpt; 
int a, b, c; 
cin >> inpt; 
a = atoi(inpt.c_str()); 
inpt = ""; 
cin >> inpt; 
b = atoi(inpt.c_str()); 
// etc... 

// Make your vector in this fashion, grabbing each integer separately then loading them 
// into a vector 
+0

輸入介紹嚮導()。看到「cin >> v [i];」 – Ross

+0

哦,我的不好,所以問題是他傳遞的是一個矢量,而不是指向矢量的指針。這導致編輯範圍不斷變化的變量。渲染它無效 –

+0

這是問題之中的是。 cin僅用於局部變量...傳遞給函數的數據不被使用。傳遞的數據是通過值傳遞的,而不是引用...等等 – Ross

相關問題